微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 SAF存储访问框架从下载文件夹访问或列出非媒体内容

如何解决使用 SAF存储访问框架从下载文件夹访问或列出非媒体内容

我正在尝试使用SAF下载文件夹中列出非媒体内容

示例代码

public void openDirectory(Uri uriToLoad) {
    // Choose a directory using the system's file picker.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

    // Provide read access to files and sub-directories in the user-selected
    // directory.
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // Optionally,specify a URI for the directory that should be opened in
    // the system file picker when it loads.
    intent.putExtra(EXTRA_INITIAL_URI,uriToLoad);

    startActivityForResult(intent,500);
}

但是当我使用 SAF 打开下载文件夹时,我看到“使用此文件夹”按钮被禁用。

我还从 android 文档中注意到他们引入了新的 MediaStore.Download

https://developer.android.com/reference/android/provider/MediaStore.Downloads

我尝试使用,但我看到游标计数为零。什么时候应该使用上面的api?

解决方法

But when i open the Download Folder using SAF i see the "use this folder" button is disable.

这是因为在 Android-11 中,通过 ACTION_OPEN_DOCUMENT_TREE 访问下载文件夹受到限制。

You can no longer use the ACTION_OPEN_DOCUMENT_TREE intent action to request access to the following directories:

The root directory of the internal storage volume.
The root directory of each SD card volume that the device manufacturer considers to be reliable,regardless of whether the card is emulated or removable. A reliable volume is one that an app can successfully access most of the time.
The Download directory.

参考 https://developer.android.com/about/versions/11/privacy/storage#file-directory-restrictions

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。