com.android.externalstorage.documents 位于非主卷上的 URI

Uri with com.android.externalstorage.documents located on non primary volume

我遇到了问题。我有 Uri 内容如下:

content://com.android.externalstorage.documents/document/[volume_uuid]:[file_path]

我可以通过 StorageManager.getStorageVolumes()

找到 StorageVolume

如果卷是主要的:StorageVolume.isPrimary() - 我知道我可以将路径解析为:

Environment.getExternalStorageDirectory() + "/" + file_path;

但是如果卷不是主要的,如何定位路径?最后,我需要通过它的绝对路径引用普通的 File 对象。

P.S。可以肯定的是,我确实拥有所有必要的权限:通过清单声明的静态或运行时声明的。

I understand that I can parse path as

不可靠。您假设代码的特定内部实现可能因设备制造商和 OS 版本而异。

In the end I need to have plain File object referenced through it's absolute path.

第 1 步:使用 ContentResolveropenInputStream()Uri

标识的内容进行 InputStream

第 2 步:在您控制的某个文件上打开 FileOutputStream(例如,在 getCacheDir() 中)

第 3 步:将内容从 InputStream 复制到 FileOutputStream

第 4 步:使用生成的文件,不再需要时将其删除

For sure I do have all necessary permission

当然你没有,因为你没有权限访问可移动存储上的文件,在非常特定的位置之外(例如,getExternalFilesDirs() 返回的第二个和后续路径)。