androidxamarin.formsstorageunauthorizedusb-otg

Xamarin.Forms Android cannot access USB OTG folder


I'm on an Android 5.0 device where a usb mass storage is mounted at "/storage/usbotg" via USB OTG cable. Unfortunately from my app I only have read access to that folder. Please note that I set the write external storage permission as I'm able to write to device storage.

I post the following code as reference:

string Root = "/storage/usbotg";                        
string[] Dirs = Directory.GetDirectories(Root);

string NewFolder = Path.Combine(Root, "NewFolder");          
Directory.CreateDirectory(NewFolder);

This gives me an exception on the last line (but I'm able to list subdirectories in Dirs)

Exception: System.UnauthorizedAccessException

Exception Message: Access to the path "/storage/usbotg/NewFolder" is denied.

If I use:

string Root =  "/storage/emulated/0"; 

everything is working fine and the "NewFolder" is created.

What I'm missing? How can I write to that folder?

I'm using Xamarin.Forms 2.5.0

Thanks for your help


Solution

  • Accessing data outside your application's private storage using the file system is more restricted with each Android version.

    The recommended options are :

    A (much) more detailed explanation by Mark Murphy.