in android 11(sdk 30) ,how to upload pdf file from downloads folder? i am getting permission denied EACCES message

Viewed 2430

it was working fine in android 10 with requestLegacyExternalStorage="true" tag. the app is like whatsapp, so that user can select file/image/location and send it to another user. Image and location are working fine, but i am getting error when uploading documents. I don't think my app qualifies for MANAGE_EXTERNAL_STORAGE permission. So if you have any solution, please share.

My file chooser after getting READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE runtime permissions.

    Intent intent = new Intent();
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, "Select File"), 1);

in OnActivityResult i am getting the document in 'file' that is global variable.

       Uri uri = data.getData();
       // Log.e("cs","uri =>"+uri);

        file = new File(uri.getPath());

        final String[] split = file.getPath().split(":");//split the path.
        String filePath = split[1];//assign it to a string(your choice).

        //Log.e("cs","filepath=>"+filePath);

        file = new File(filePath);

       // Log.e("cs","file=>"+file.exists());

the problem arise when i am uploading the file

    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

so in retrofit it goes to onFailure and i am getting error :: open failed: EACCES (Permission denied) .

1 Answers

The solution you need to add

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

in the manifest file and try to get the file access permission in the android 11 phones. Then you will open and read the file from the storage.

Related