Android 11 (R) 文件路径访问

标签 android android-11

根据 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:

  1. Declare the MANAGE_EXTERNAL_STORAGE permission.
  2. 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列检索文件路径。

请注意:
  • 我知道一个 UriMediaStore 返回并且不指向文件。
  • 我知道我可以将文件复制到我的应用程序目录并将其传递给 FFmpeg ,但我可以在 Android R 之前做到这一点。
  • 我过不了FileDescriptorFFmpeg我不能使用/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 上提问后,我得出以下结论:

  • 在 Android R 上,File移除了在 Android Q 中添加的限制。所以我们可以再次访问File对象。
  • 如果您的目标是 Android 10 > 并且想要访问/使用文件路径,则必须在 list 中添加/保留以下内容:
    android:requestLegacyExternalStorage="true"
    

    这是为了确保文件路径在 Android 10(Q) 上有效。在 Android R 上,此属性将被忽略。
  • 不要使用 DATA 列插入或更新媒体存储,使用 DISPLAY_NAMERELATIVE_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_TREEACTION_OPEN_DOCUMENT请求用户从 Android/data/ 中选择单个文件的 Intent 操作, Android/obb/和所有子目录。
  • 建议只使用File需要执行“寻找”时的对象,例如使用 FFmpeg 时, 例如。
  • 您只能使用数据列访问磁盘上的文件。您应该相应地处理 I/O 异常。


  • 如果您想访问 File或者想要一个来自 Uri 的文件路径从 MediaStore 返回, I've created a library处理您可能遇到的所有异常。这包括磁盘、内部和可移动磁盘上的所有文件。选择 File 时例如,来自 Dropbox 的 File将被复制到您具有完全访问权限的应用程序目录中,然后将返回复制的文件路径。

    关于Android 11 (R) 文件路径访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360368/

    相关文章:

    android - 如何在 Android Studio 的设计面板中使用 CustomView?

    android - 找到 com.google.android.gms :play-services:8. 4.0,但 google-services 插件需要 8.3.0 版本

    android - GPS跟踪的前台服务在一段时间后停止工作

    android - 如何在我的 Android 11 应用中隐藏状态栏?

    java - 无法将照片上传到服务器android retrofit 2

    javascript - 输入类型 ="file"拍照选项

    安卓 11 : how to get current location

    android - 无法启动 AVD

    当应用程序进入后台时,Android 11(API 级别 30)onTaskRemoved 在前台服务中触发

    android - React Native 与现有 Android 应用集成闪退