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);
}