问题
I am using StorageVolume API to retrieve URI for both SDCard and USBStorage by following
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
for (StorageVolume storageVolume : storageManager.getStorageVolumes())
{
if (!storageVolume.isPrimary())
{
Intent accessIntent = storageVolume.createAccessIntent(null);
startActivityForResult(accessIntent, 11);
}
}
In onActivityResult I am able to retrieve the URI, saving it to the SharedPreferences and taking Persisting Permission. The thing is I am not able to distinguish the SDCard's URI from USBStorage's URI. It also applies for the URI's which are returned from Storage Access Framework. How could I really know that the user has really selected the expected storage not the wrong one.
Things Tried
- I tried to send some data with the
createAccessIntentbut wasn't able to retrieve values inonActivityResult. Just wanted to know that why it doesn't have the extras that I put to theIntentreturned bycreateAccessIntent. - I tried to
context.getExternalFilesDirs(null)[]but it returnsnullfor theSDCarditem. I also checked the state ofSDCardwhich was mounted. Obviously, I took runtime permission for storage from user. - Tried to assume that
storageManager.getStorageVolumes()would return list of all the external storage with order ofUSBStorageat the first place as it does on my device but doesn't on all devices. It lists items randomly. - I also tried to use hack something like
Environment.getRootDirectory()and get its parent and navigate to theSDCardon the root folder but it only returns the child elements of the mount point ofSDCardandUSBStorageonMarshmellownot beyond that. I mean it simply returns null onNougat+ devices.
来源:https://stackoverflow.com/questions/53845539/distinguish-sdcard-uri-from-usbstorage-uri-returned-by-saf-or-storagevolume-api