Given a File
or a Uri
, how can I reliably determine whether or not the file is located on a USB mass storage device connected to the phone's USB port?
My test phone, a Samsung Galaxy S7 Edge, has a USB mass storage device connected, an internal (physical) micro SD card, and internal storage. When I query Context.getExternalCacheDirs()
, I see all three of them:
/storage/emulated/0/Android/data/...
/storage/A2B3-15F3/Android/data/...
/storage/3535-3038/Android/data/...
How can I determine which one is the USB mass storage device? (It's A2B3-15F3, by the way).
Research
I looked at this answer (the StorageHelper.resolveType()
method), but as you can see, the device's mounted file path does not contain the string "usb".
I then tried using the StorageManager
service. With the devices above we can get a StorageVolume` using:
StorageManager storageManager = context.getSystemService(StorageManager.class);
StorageVolume volume = storageManager.getStorageVolume(context.getExternalCacheDirs()[1]);
This is where things got a little odd. The StorageVolume
API docs and my local copy of the file in the Android 25 sources don't appear to be much help to me, but when I execute the above code in the Android Studio debugger the object contains an mSubSystem
field that describes the three devices above as "fuse", "usb" and "sd" respectively. Here is an example:
Why do I see this information in the debugger, but not the source? Could this field be specific to the Android distribution for this particular phone?