0

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

  • Which file browser app did you use? – greenapps Jan 09 '18 at 18:18
  • Please use `try-catch` to get the `Message`. – Robbit Jan 10 '18 at 02:22
  • ES File Explorer is the the file browser I'm using, I think also others will work too. Exception is System.UnauthorizedAccessException Exception message is Access to the path "/storage/usbotg/NewFolder" is denied. – Marco Bosetti Jan 10 '18 at 08:58
  • Micro SD cards and usb drives are readonly for Android > 4. Only one app specific directory is writable. Have a look at getExternalFilesDirs(). It seems ES File Explorer knows some tricks so it can write. If you want to write to the whole sd card / storage then use the storage access framework. And try other file explorer apps. And you did not mention if you can write on a micro sd card. – greenapps Jan 10 '18 at 09:04
  • Thanks for your help greenapps, when using getExternalFilesDir with USB OTG plugged it only returns one path on device storage: /storage/emulated/0/Android/data/com.my.app/files, also getExternalFilesDirs documentation states "The returned paths do not include transient devices, such as USB flash drives connected to handheld devices." – Marco Bosetti Jan 10 '18 at 09:55
  • Tried with another File Manager, Total Commander, cannot write to usb otg from there. Tried again with ES File Explorer cannot write from there too! Sorry I was wrong when said I can. So, seems that on this device I only have read access to usb otg. Is that normal? What is the correct way of getting usb otg path from android? – Marco Bosetti Jan 10 '18 at 10:22
  • With SD card inserted I got an additional entry returned by getExternalFilesDirs() pointing to a specific folder where I have write permissions. – Marco Bosetti Jan 10 '18 at 14:24

1 Answers1

1

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

The recommended options are :

  • Use getExternalFilesDirs(), getExternalCacheDirs, ... : this gives you one or more directories specific to you application (usually directories named after the package name). This does not work for removable media, unless they're adopted.

  • Use the Storage Access Framework : ask the user (using ACTION_OPEN_DOCUMENT_TREE) to choose the storage root. You can then manage the content of the picked directory through the SAF API. You can persist the permission, so you only need to ask the user once. It seems that it is what ES File explore does to get write permission.

A (much) more detailed explanation by Mark Murphy.

bwt
  • 17,292
  • 1
  • 42
  • 60