ACTION_OPEN_DOCUMENT 抛出“打开文件失败:EACCES(权限被拒绝)”尝试将文件上传到服务器时在 Android 11 及更高版本上出现错误

发布于 2025-01-16 15:13:01 字数 2049 浏览 7 评论 0原文

以下是我向用户请求的许可。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="30"
    tools:ignore="ScopedStorage" />

通过使用这些权限,我可以使用 android 9 及更低版本中的系统文件选择器获取任何文件。

对于 android 10 设置

    android:requestLegacyExternalStorage="true" 

工作正常。

但对于 android 11 及更高版本,当用户打开系统文件选择器时,我使用下面的代码触发它。

private fun openAttachView() {
    val mimeTypes = arrayOf (
         "application/pdf"
    )

    val intent =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.apply {
            addCategory(Intent.CATEGORY_OPENABLE);
            type = if(mimeTypes.size == 1) {
                mimeTypes[0]
            } else {
                "*/*"
            }
            putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        }
        intent
    } else {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        val mimeTypesStr = StringBuilder()
        for (type in mimeTypes) {
            mimeTypesStr.append(type).append("|")
        }
        intent.type = mimeTypesStr.substring(0, mimeTypesStr.length - 1);
        intent
    }

    startActivityForResult(Intent.createChooser(intent, "ChooseFile"), ATTACH_DOC_FILE);

}

如果用户从任何文件夹中选择图像,我就可以获取其路径,甚至也可以将其上传到服务器。 但是,如果用户选择任何其他文件(例如 pdf),那么我仍然能够获取该特定文件的路径/uri,但是当我尝试将其上传到服务器时,我收到以下错误。

打开文件失败:EACCES(权限被拒绝)

但是当我使用

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

它时工作正常,但是谷歌播放拒绝我的应用程序说你可以使用

存储访问框架

,并在存储访问框架中明确提到使用

客户端应用程序 - 调用 ACTION_CREATE_DOCUMENT 的自定义应用程序, ACTION_OPEN_DOCUMENT 和 ACTION_OPEN_DOCUMENT_TREE 意图操作和 接收文档提供者返回的文件。

我已经在使用了。

任何建议或修复请提及,如果需要任何澄清请评论我将不断更新问题。

谢谢。

Below are the permission im requesting from the user.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="30"
    tools:ignore="ScopedStorage" />

By using these permission I'm able to get any file using the system file picker in android 9 and less.

For android 10 setting the

    android:requestLegacyExternalStorage="true" 

work fine.

But for android 11 and higher when user opens the system file picker which i trigger using the below code.

private fun openAttachView() {
    val mimeTypes = arrayOf (
         "application/pdf"
    )

    val intent =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.apply {
            addCategory(Intent.CATEGORY_OPENABLE);
            type = if(mimeTypes.size == 1) {
                mimeTypes[0]
            } else {
                "*/*"
            }
            putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        }
        intent
    } else {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        val mimeTypesStr = StringBuilder()
        for (type in mimeTypes) {
            mimeTypesStr.append(type).append("|")
        }
        intent.type = mimeTypesStr.substring(0, mimeTypesStr.length - 1);
        intent
    }

    startActivityForResult(Intent.createChooser(intent, "ChooseFile"), ATTACH_DOC_FILE);

}

If the user choose an image from any folder I'm able to get its path and even able to upload it to the server as well.
But if the user chooses any other file lets say a pdf then I'm still able to get the path/uri for that particular file but when i try to upload it to the server i get the following error.

open file failed: EACCES (Permission denied)

But when i use

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

it works fine, but then google play is rejecting my app by saying you can use

storage access framework

and in the storage access framework its clearly mention to use

Client app—A custom app that invokes the ACTION_CREATE_DOCUMENT,
ACTION_OPEN_DOCUMENT, and ACTION_OPEN_DOCUMENT_TREE intent actions and
receives the files returned by document providers.

which i had been already using.

Any suggestions or fixes please mention, if any clarification is needed please comment I will keep updating the question.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文