Distinguish SDCard URI from USBStorage URI returned by SAF or StorageVolume API

穿精又带淫゛_ 提交于 2019-12-23 03:05:07

问题


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

  1. I tried to send some data with the createAccessIntent but wasn't able to retrieve values in onActivityResult. Just wanted to know that why it doesn't have the extras that I put to the Intent returned by createAccessIntent.
  2. I tried to context.getExternalFilesDirs(null)[] but it returns null for the SDCard item. I also checked the state of SDCard which was mounted. Obviously, I took runtime permission for storage from user.
  3. Tried to assume that storageManager.getStorageVolumes() would return list of all the external storage with order of USBStorage at the first place as it does on my device but doesn't on all devices. It lists items randomly.
  4. I also tried to use hack something like Environment.getRootDirectory() and get its parent and navigate to the SDCard on the root folder but it only returns the child elements of the mount point of SDCard and USBStorage on Marshmellow not beyond that. I mean it simply returns null on Nougat+ devices.

来源:https://stackoverflow.com/questions/53845539/distinguish-sdcard-uri-from-usbstorage-uri-returned-by-saf-or-storagevolume-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!