0

So, I want to read a very specific file from any connected USB OTG drive. It's a text file that should be stored on a USB stick and I need to read that file, which has a specific name, say myFile.txt. While I understand this kind of getting user input is dumb and not user-friendly, it's what I'm told to implement.

But my problem is, that there are many mount points for USB Drives. on my phone it's /storage/USBStorage1, while on my friend's it's /storage/[HEX_ID]. I tried to get all of them, but it's not possible. in Nexus phones it mounts on /mnt/media_rw/[HEX_ID] which requires root access.

I searched around a little and found SAF, but I didn't find anything on how to use it to open any file without user's direct selection. I don't want the picker to show up. I just want to check if [USB_OTG_PATH]/myFile.txt exists or not, and if it exists I want to read it.

And on a sidenote, do I need to have root acces to read /mnt folders (except media_rw)?

Mehdi
  • 65
  • 4
  • On modern systems it will be one of the `File` objects delivered by getExternalFilesDirs()`[. – greenapps Nov 15 '16 at 15:42
  • @greenapps: Only if the file happens to be in one of those directories. The OP seems to want this file to be at an arbitrary location. – CommonsWare Nov 15 '16 at 15:45
  • @CommonsWare: From that File object OP can derive the root of the drive. From there on he adds his path. The root is the part before `/Android/`. – greenapps Nov 15 '16 at 15:49
  • @greenapps: At which point, he starts getting `EACCESS` and related errors. Apps have read/write access to the *specific* directories returned by `getExternalFilesDirs()` and kin, but cannot navigate outside of them. – CommonsWare Nov 15 '16 at 15:50
  • @CommonsWare: OP will have requested all necessary permissions already. Like READ_EXTERNAL_STORAGE. – greenapps Nov 15 '16 at 15:53
  • @greenapps: `READ_EXTERNAL_STORAGE` is for external storage. External storage is not removable storage, and the OP is interested in removable storage. `READ_EXTERNAL_STORAGE` has no impact on USB OTG devices. – CommonsWare Nov 15 '16 at 15:54
  • @CommansWare: Ok. Thanks. But i have never seen an Android device where i could not read the whole OTG disk. Writing indeed is another story. – greenapps Nov 15 '16 at 15:56
  • @CommonsWare: As far as i know READ_EXTERNAL_STORAGE is certainly needed for a micro SD card. – greenapps Nov 15 '16 at 16:01
  • @greenapps: No, it's not in `getExternalFilesDirs()`. That returns the directory of files stored on External Storage. What I want is something named `getRemovableStorageDirs()` that returns the directory of mounted USB sticks. – Mehdi Nov 15 '16 at 16:26
  • 1
    You are aware of the TWO functions? `getExternalFilesDir()` and `getExternalFilesDirs()`? Use the latter. And you should have mentioned the Android version of course if it did not work. – greenapps Nov 15 '16 at 16:36
  • `but I didn't find anything on how to use it to open any file without user's direct selection.`. You only once have to let the user choose the root of that usb drive. After that your app can read the whole -same- drive for the lifetime of your app without user intervention. – greenapps Nov 15 '16 at 16:40
  • And for Android 7 StorageManager and StorageVolumes can be used. – greenapps Nov 15 '16 at 16:42

1 Answers1

0

I didn't find anything on how to use it to open any file without user's direct selection

That is because there is no option for this.

I just want to check if [USB_OTG_PATH]/myFile.txt exists or not, and if it exists I want to read it.

That is not supported.

If your file is located in one of the directories returned by getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs(), then you can access it directly using normal Java file I/O. However, AFAIK, that directory needs to be created as part of running your app — another developer ran into problems trying to create the directories ahead of time. So, for your use case, this approach is unlikely to be practical, though with luck I am wrong and it proves useful to you.

do I need to have root acces to read /mnt folders (except media_rw)?

In general, yes, though in practice the answer varies by device, Android OS version, etc.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • No, my file is not in the External directories. It's supposed to be in a USB stick and the app should only work when the stick is plugged in. – Mehdi Nov 15 '16 at 16:20
  • @III.6: "It's supposed to be in a USB stick" -- `getExternalFilesDirs()`, `getExternalCacheDirs()`, and `getExternalMediaDirs()` all return a `File[]`. If that array has 2+ entries, the second and subsequent ones are on removable storage, such as your USB OTG drive. Those directories you can read and write to without permissions, using ordinary Java file I/O. – CommonsWare Nov 15 '16 at 16:40
  • But it doesn't. On my phone each of them returns an array of size 1 and and all of them have the prefix `/storage/emulated/0/Android/`, while my stick is mounted in `/storage/USBstorage1`. – Mehdi Nov 15 '16 at 16:54
  • @III.6: Does the device recognize the drive, besides your advertised mount point? For example, do you have a notification for this drive, with an "Explore" action, that works? – CommonsWare Nov 15 '16 at 16:57
  • Yes, it does. There's a notification titled "USB storage connected" and it says "Tap to remove USB storage safely". But no "Explore" option. Update: Also tested on a Nexus 5, which shows a notification with "Explore" and "Eject" options. but still the arrays are of size 1. – Mehdi Nov 15 '16 at 17:00
  • @III.6: The more important point is whether you can browse that storage. On Android 6.0+ (and perhaps 5.x, I forget), there should be an "Explore" action on the notification that brings up an activity where you can browse the drive, copy files to external storage, etc. – CommonsWare Nov 15 '16 at 17:02
  • Yes, it's possible. FX File Manager and LG's stock file manager find it too. But on Nexus 5, FX doesn't. though the "Explore" option in the notification works. – Mehdi Nov 15 '16 at 17:03
  • @III.6: If you are not getting 2+ items from `getExternalFilesDirs()` and kin on the Nexus 5 (and on the LG, if it is running Android 4.4+), I cannot explain that result. – CommonsWare Nov 15 '16 at 17:07
  • So, what do I do? test it on other devices? – Mehdi Nov 15 '16 at 17:18
  • @III.6: "So, what do I do?" -- I don't really have much to suggest. Your overall use case (detect a special file on removable storage without user intervention) is not a use case that Android supports especially well. I would explore alternative solutions for whatever you are trying to achieve by this file-detection system. – CommonsWare Nov 15 '16 at 17:20
  • It's not really in my hands. I'm just the guy who has to implement it. Thanks for you information anyway :) – Mehdi Nov 15 '16 at 17:34