How to read USB OTG on Android 11

Viewed 2539

I want to read an OTG USB on Android 11 I'm able to list files like this on Android 9 :

val path = File("/storage")

I can list files from the external memory in Android 11 but not the USB OTG :

val path = Environment.getExternalStorageDirectory()

The question is why can I read external memory but not OTG USB on Android 11 ? I want to be able to read USB OTG on Android 11.

1 Answers

Use getExternalFilesDirs(null) or ACTION_OPEN_DOCUMENT_TREE.

getExternalFilesDirs() will return one, two or three items depending on removable micro sd card inserted and usb otg pen drive attached.

You get the path to the partitions by removing the Android directory and further from the obtained paths.

Update. I had not connected a usb pen drive before to my three days old Android 11 device.

Sadly it was not mentioned in getExternalFilesDirs() which used to be the case at lower Android versions. The micro SD card is.

But.. full acces using SAF with ACTION_OPEN_DOCUMENT_TREE. One can even choose root of drive.

Related