How to read file from USB drive in Xamarin.Android platform

sasikumar123sasikumar123 ✭✭Member ✭✭
edited July 2018 in Xamarin.Android

Hi All,

We use our own embedded device running on Android 7.1.1 Nougat. I am not able to access the PDF file

from the USB drive. The device is automatically mounted on /mnt/media/( device name ).

Can you please help me how to read the file from USB drive.

We have tried different Xamarin Forums and discussion suggestion related to this topic, but we are not able to access the file from USB drive.

We have provided all the permission the USB drive, and the exception has been throws while accessing file.

var mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    UsbManager usbManager = (UsbManager)GetSystemService(Context.UsbService);

    var deviceList = usbManager.DeviceList;
    IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
    if (deviceIterator.Count() > 0)
    {
        UsbDevice device = deviceIterator.ElementAt(0);
        usbManager.RequestPermission(device, mPermissionIntent);
        if (usbManager.HasPermission(device))
        {
            if (usbManager.HasPermission(device))
            {
                System.Diagnostics.Debug.WriteLine("HasPermission");
                UsbDeviceConnection connection = usbManager.OpenDevice(device);
                if( connection != null)
                {
                    System.Diagnostics.Debug.WriteLine(" connection is not null");
                    var model = device.DeviceName;
                    string inline = null;
                    BufferedReader reader = null;
                    reader = new BufferedReader(new FileReader("/mnt/media_rw/C227-875D/test/hello.txt"));
                   System.Diagnostics.Debug.WriteLine("no exception occured");
                   while ((inline = reader.ReadLine()) != null)
                   {
                        System.Diagnostics.Debug.WriteLine(inlne);

                   }
              }
         }

Please help me to resolve this.

Thanks.

Answers

  • JGoldbergerJGoldberger Xamurai USMember, Forum Administrator, Xamarin Team, University Xamurai

    @sasikumar123

    I think you may have hit a security limitation on Android 6.0 and above:

    https://source.android.com/devices/storage/traditional

    USB media support
    Android 6.0 supports portable storage devices which are only connected to the device for a short period of time, like USB flash drives. When a user inserts a new portable device, the platform shows a notification to let them copy or manage the contents of that device.

    In Android 6.0, any device that is not adopted is considered portable. Because portable storage is connected for only a short time, the platform avoids heavy operations such as media scanning. Third-party apps must go through the Storage Access Framework to interact with files on portable storage; direct access is explicitly blocked for privacy and security reasons.

    I believe that you will either need to adopt the drive, locking the drive to that particular android device, or use the Storage Access Framework which will require user interaction to access files on the external USB drive.

Sign In or Register to comment.