2

I am trying to upload file (which could be any file .pdf,.doc,.jpeg,.png etc) from Android app (from multiple location eg - documents, downloads etc) to server using Retrofit (Java). I have created intent to open file chooser:

private void createIntentToPickFile() {
    Intent selectFileIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    selectFileIntent.setType("*/*");
    selectFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
    launchSelectFileActivity.launch(selectFileIntent);
}

this is the ActivityResult method:

ActivityResultLauncher<Intent> launchSelectFileActivity = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                Uri selectedImageUri = data.getData();
                Cursor returnCursor =
                        getContentResolver().query(selectedImageUri, null, null, null, null);
                int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                returnCursor.moveToFirst();
                Log.e("File Name is :: ", returnCursor.getString(nameIndex));
                String fileName = returnCursor.getString(nameIndex);
                returnCursor.close();

                // MEDIA GALLERY
                String selectedImagePath = Util.getPathFromURI(getApplicationContext(), selectedImageUri);
                File videoFile = new File(selectedImagePath);
                uploadFile(videoFile, fileName, category);
            }
        });

and here I am preparing my multipart request to upload file to server:

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), filePath);
RequestBody uploadedBy = RequestBody.create(MediaType.parse("text/plain"),
        SharedPreference.getData(getApplicationContext(), Constant.USERID));
RequestBody auditId = RequestBody.create(MediaType.parse("text/plain"),
        String.valueOf(customerInfo.getAuditId()));
RequestBody categoryType = RequestBody.create(MediaType.parse("text/plain"), category);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", fileName, requestBody);
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<Void> call = apiInterface.uploadAuditFiles(part, uploadedBy, auditId, categoryType);

I am able to get all the file details but when I try to upload file it gives me this error

java.io.FileNotFoundException: /storage/emulated/0/Download/2952515496054.pdf: open failed: EACCES (Permission denied)

I know I have read about scoped storage in Android 10, but could find any solution related to file upload.

Can anyone guide how to solve this permission issue.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Doctor Who
  • 747
  • 1
  • 5
  • 28
  • 2
    A `Uri` is not a file, and you do not need a file to upload the content. The accepted answer on the duplicate question links to three separate implementations of a `RequestBody` that works with a `Uri`. – CommonsWare Jul 06 '21 at 18:48
  • If anyone still looking for solution, I have uploaded files in github, please have a look https://github.com/behappiest/scoped-storage-EACCESS-ERROR-SOLUTION – Doctor Who Jul 07 '21 at 05:30

0 Answers0