6

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) .

Dhaval Vyas
  • 61
  • 1
  • 4
  • https://stackoverflow.com/questions/66964812/android-11-open-failed-eacces-permission-denied did you check this thread – Edgar Jun 05 '21 at 05:36
  • hi, i checked the thread but , i am already using ACTION_OPEN_DOCUMENT and i don't think MANAGE_EXTERNAL_STORAGE solves the issue. This app might be rejected by Play Store and this app is already in production. – Dhaval Vyas Jun 05 '21 at 05:53
  • Show your code. As if the user selected a file using ACTION_OPEN_DOCUMENT you do not need any further permission to use that file. – blackapps Jun 05 '21 at 08:33
  • @blackapps please check the code in edit – Dhaval Vyas Jun 05 '21 at 09:03
  • You are not showing what you obtained in onActivityResult. Not a `file` we know all. Please post complete code so we know what you do wrong. Well we already know .. but you should show it ;-) At this moment you did not even tell what you did. – blackapps Jun 05 '21 at 09:09
  • @blackapps please check on activityresult code. if you need anything else please tell. – Dhaval Vyas Jun 05 '21 at 09:24
  • `file = new File(uri.getPath());` That is nonsense code. If you had used file.exists() to check if the file existed you would not even have started an upload. Have a look too at uri.getPath() and file.getAbsolutePath() to realize that that is an impossible file system path. – blackapps Jun 05 '21 at 10:07
  • please check the edited onactivityresult , i am getting file exists as true every time also in retrofit multipartbody convertion , after that i am getting error in retrofit on failure – Dhaval Vyas Jun 05 '21 at 10:57
  • Is this issue solved? what solution u got? I'm also getting EACCES (Permission denied) in retrofit onFailure after I start targetting app to Android 11 – Nadimuddin Aug 05 '21 at 18:52
  • Hello. Did you solve this problem ? – Zappy.Mans Oct 12 '21 at 09:39
  • Hi!Did you find any solution? – Tetereou Mouhammad Abdourazak Aug 14 '22 at 11:07

1 Answers1

1

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.