根据 docs 文件路径访问权限在 Android R 中被授予:
Starting in Android 11, apps that have the READ_EXTERNAL_STORAGE permission can read a device's media files using direct file paths and native libraries. This new capability allows your app to work more smoothly with third-party media libraries.
问题是我无法从
MediaStore
获取文件路径,那么我们应该如何读取我们无法访问/检索的文件路径?有没有办法,我不知道,我们可以从 MediaStore
获取文件路径?此外,the docs say the following :
All Files Access
Some apps have a core use case that requires broad file access, such as file management or backup & restore operations. They can get All Files Access by doing the following:
- Declare the MANAGE_EXTERNAL_STORAGE permission.
- Direct users to a system settings page where they can enable the Allow access to manage all files option for your app.
This permission grants the following:
- Read access and write access to all files within shared storage.
- Access to the contents of the MediaStore.Files table.
但我不需要所有文件访问权限,我只希望用户从
MediaStore
中选择一个视频并将文件路径传递给FFmpeg
(它需要一个文件路径)。我知道我不能再使用 _data
列检索文件路径。请注意:
Uri
从 MediaStore
返回并且不指向文件。 FFmpeg
,但我可以在 Android R 之前做到这一点。FileDescriptor
至FFmpeg
我不能使用/proc/self/fd/
(我从 SD 卡中选择文件时得到 /proc/7828/fd/70: Permission denied
),请查看 this issue . 那我该怎么办,我错过了什么吗?
can read a device's media files using direct file paths and native libraries
是什么意思?
最佳答案
在 issuetracker 上提问后,我得出以下结论:
File
移除了在 Android Q 中添加的限制。所以我们可以再次访问File
对象。 android:requestLegacyExternalStorage="true"
这是为了确保文件路径在 Android 10(Q) 上有效。在 Android R 上,此属性将被忽略。
DISPLAY_NAME
和 RELATIVE_PATH
,这里是一个例子:ContentValues valuesvideos;
valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "YourFolder");
valuesvideos.put(MediaStore.Video.Media.TITLE, "SomeName");
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, "SomeName");
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ACTION_OPEN_DOCUMENT_TREE
或 ACTION_OPEN_DOCUMENT
请求用户从 Android/data/
中选择单个文件的 Intent 操作, Android/obb/
和所有子目录。 File
需要执行“寻找”时的对象,例如使用 FFmpeg
时, 例如。 如果您想访问
File
或者想要一个来自 Uri
的文件路径从 MediaStore
返回, I've created a library处理您可能遇到的所有异常。这包括磁盘、内部和可移动磁盘上的所有文件。选择 File
时例如,来自 Dropbox 的 File
将被复制到您具有完全访问权限的应用程序目录中,然后将返回复制的文件路径。
关于Android 11 (R) 文件路径访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360368/