Android 11 无法从本地存储读取非媒体文件

问题描述

我在 Android 11 中遇到文件选择器问题。图像或视频等文件可读,但文档等不可读。 我仔细检查了存储读取权限。

示例

uri = content://com.android.providers.downloads.documents/document/103 
someFile = /storage/emulated/0/Download/IMG_20210310_232019_1.jpg
someFile.canRead -> true

但是

uri = content://com.android.providers.downloads.documents/document/msf:1390 
someFile = /storage/emulated/0/Download/file-example_PDF_1MB.pdf
someFile.canRead() -> false

然后:

com.amazonaws.AmazonClientException: Unable to calculate MD5 hash: /storage/emulated/0/Download/file-example_PDF_1MB.pdf: open Failed: EACCES (Permission denied)

这是我的FILE_PROVIDER_PATHS

<paths>
    <cache-path
        name="camera"
        path="camera/" />
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_sd_files"
        path="/storage/" />
</paths>

文件选择功能

fun pickFile(fragment: Fragment) {
    val intent = Intent()
    val mimeTypes = arrayOf(
        "image/*","application/msword","text/*","application/vnd.ms-xpsdocument","application/vnd.ms-works","application/xhtml+xml","application/vnd.openxmlformats-officedocument.wordprocessingml.document",// .doc & .docx
        "application/vnd.ms-word.template.macroEnabled.12",// .dotm
        "application/vnd.openxmlformats-officedocument.wordprocessingml.template",// .dotx
        "application/vnd.ms-powerpoint","application/vnd.openxmlformats-officedocument.presentationml.presentation",// .ppt & .pptx
        "application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",// .xls & .xlsx
        "application/pdf"
    )
    intent.type = "*/*"
    intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes)
    intent.action = Intent.ACTION_GET_CONTENT
    intent.putExtra("return-data",true)
    fragment.startActivityForResult(
        intent,SELECT_FILE_REQUEST
    )
}

onActivityResult:

override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
    if (requestCode == ChatFileUtils.SELECT_FILE_REQUEST) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                pickiT.getPath(data.data,Build.VERSION.SDK_INT)
            }
        }
    }
    super.onActivityResult(requestCode,resultCode,data)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)