3

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.

zzz
  • 83
  • 1
  • 9
  • `The question is why can I read the sd card` /storage is not a removable micro sd card. Moreover you cannot read /storage. – blackapps Feb 24 '21 at 13:03
  • `The question is why can I read the sd card` That is external memory which is in your device. It has noting to do with a removable micro SD card. – blackapps Feb 24 '21 at 13:25
  • Can you list from removable micro sd card already? Well if you inserted one.. – blackapps Feb 24 '21 at 13:31
  • `why can I read external memory but not OTG USB on Android 11 ?` Well you use a path to external memory. So no wonder.. I think you should yet once reword your problem. – blackapps Feb 24 '21 at 13:33

1 Answers1

0

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.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Thank you! I used getExternalFilesDirs(null) it worked on a Huawei device but for some reason, it doesn't show the path for USB OTG pen drive in google pixel device any idea why? both have the same version. – zzz Mar 02 '21 at 08:25
  • ACTION_OPEN_DOCUMENT_TREE – blackapps Mar 02 '21 at 09:09
  • I'm using ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION I don't want to use ACTION_OPEN_DOCUMENT_TREE because I want to customize my file manager app. – zzz Mar 02 '21 at 09:14
  • Well if the usb otg drive is only accessable using saf then you have to use saf. That all files access does not suddenly open that drive for you. And if you make a file manager app you should add a saf explorer too. – blackapps Mar 02 '21 at 09:18
  • Got it! Thank you! – zzz Mar 02 '21 at 09:21
  • @ blackapps Do you have any idea why Libaums Library is able to read my OTG USB on Android 11? even though it uses getExternalFilesDirs(null) ? – zzz Mar 04 '21 at 11:06
  • Well on some devices the usb otg drive is mentioned in getExternalFilesDirs(). Nothing against that. You can easily check if it is. – blackapps Mar 04 '21 at 13:50