1 year ago

#329205

test-img

Tehleel Mir

ACTION_OPEN_DOCUMENT throwing "open file failed: EACCES (Permission denied)" error on android 11 and higher when trying to upload the file to server

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.

android

android-permissions

android-file

android-11

storage-access-framework

0 Answers

Your Answer

Accepted video resources