0

I have a Xamarin Android application (target framework Android 7.1 (Nougat)), with the permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE and I want to read and write files on an USB stick.

I read the proc/mounts system file to find the volume of my USB stick (as explained in this thread : How can I get the list of mounted external storage of android device) but when I try to write a file to the USB stick, I then get an "access denied" exception.

Then my question is, is it possible to read and write files to a connected USB stick and if so how can I do this?

Here is the code I'm using to request permission to access the USB stick:

            UsbManager usbManager = (UsbManager)Global.MyActivity.GetSystemService(Context.UsbService);
            var deviceList = usbManager.DeviceList;
            IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();

            if (deviceIterator.Count() > 0)
            {
                var device = deviceIterator.ElementAt(0);

                string ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
                var mPermissionIntent = PendingIntent.GetBroadcast(Global.MyActivity.ApplicationContext, 0, new Intent(ACTION_USB_PERMISSION), 0);

                usbManager.RequestPermission(device, mPermissionIntent);
            }
DevAct
  • 95
  • 1
  • 11
  • Could you please show your code? – Grace Feng Oct 12 '17 at 01:35
  • My code is similar to the one in the thread I was talking about (except my code is C#) and to request permission to acces the USB stick I use the piece of code I added to my question. – DevAct Oct 12 '17 at 13:13

1 Answers1

0

The best method is using Android Storage Access Framework (SAF):

I have developed a free code that overcome this issue: https://github.com/madnik7/PortableStorage

Mohammad Nikravan
  • 1,543
  • 2
  • 19
  • 22