2

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.

Tehleel Mir
  • 743
  • 8
  • 27
  • 1
    You should not try to get a path for an uri. You can use the uri directly to upload the file. Also you can do away with all those permissions. – blackapps Mar 24 '22 at 05:55
  • this is how im uploading the file to other server.................(1) val file = File(FileUtils.getRealPath(context , fileUri))................(2) addFormDataPart(Constants.PRES_IMG , file.name , file.asRequestBody(MultipartBody.FORM)) – Tehleel Mir Mar 24 '22 at 06:01
  • As said before: do not use getRealPath() and also do not try to use the File class. All not needed. You can use the uri directly. For all Android versions without any permissions. – blackapps Mar 24 '22 at 06:03
  • `ACTION_OPEN_DOCUMENT throwing "open file failed: EACCES (Permission denied)"` Wrong accusation. Your error has nothing to do with ACTION_OPEN_DOCUMENT but with you messing around with a nice uri to obtain a useless path. – blackapps Mar 24 '22 at 06:12
  • But its not possible to add uri object in to the requestBody object, see this answer https://stackoverflow.com/a/34411666/14599955 its also creating a file first from the uri – Tehleel Mir Mar 24 '22 at 06:17
  • 1
    Wrong info. There are so many posts with the right solution so what made you choose this one. Not the uri directly but an inputstream for it. Google for inputstreamrequestbody. And Android Studio will tell you that possibility too i think. – blackapps Mar 24 '22 at 06:30
  • 2
    Thank you @blackapps it worked, so i converted the URI into input stream first and then that inputStream into a requestBody object and it worked fine. Thanks mate – Tehleel Mir Mar 24 '22 at 07:01

0 Answers0