0

I am trying to remove MANAGE_EXTERNAL_STORAGE, but after removing this permission I am not able to upload Multipart from the given path.

I am using this line of code to pick up the pdf file,

`Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
resultLauncher.launch(intent);`

and I am getting the path like /storage/emulated/0/Download/offer.pdf

After getting this path i am converting it into Multipart by using following code and uploading it to the server,

`File file = new File(path);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(file,      MediaType.parse("application/pdf")));`

and while uploading this on server i am getting an error which is:

java.io.FileNotFoundException: /storage/emulated/0/Documents/mSchooling/offer.pdf: open failed: EACCES (Permission denied).

One thing is that i already given read_external_storage and write_external_storage.

Please help me to resolve this problem.

I tried this code to pick up the file,

ntent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("application/pdf"); resultLauncher.launch(intent);

and trying to convert it into Multipart by using following code,

File file = new File(path); MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(file, MediaType.parse("application/pdf")));

1 Answers1

0

and I am getting the path like /storage/emulated/0/Download/offer.pdf

No you are not.

You get a nice uri with ACTION_GET_CONTENT. A content scheme.

Use that uri.

Google for inputstreamrequestbody.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • This answer help me a lot. There was a mistake while converting the Uri in RequestBody. Which is RequestBody.create(file, MediaType.parse("application/pdf")). I replace this line of code by new UriRequestBody(MediaType.parse("application/pdf"), getApplicationContext().getContentResolver(), sUri). [https://gist.github.com/gbzarelli/a7ac6d487e9c2cf3885c5b6c56cc6996 ] – Shadab Azam Jan 23 '23 at 06:33