3

I am trying to upload a pdf document and I get the following error. my android device is running on Android 11

java.io.FileNotFoundException: /storage/emulated/0/Download/Gmail - Extension quote.pdf: open failed: EACCES (Permission denied)

I have looked online and I have done everything I could,

added permission in Manfest

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

<application
    android:requestLegacyExternalStorage="true" />

When upload pdf button is clicked I check if there is a permission this way

 uploadFileBtn.setOnClickListener {
            // Check if read storage permissions are already granted
            if (askForStoragePermissions(FILE_BROWSER_PERMISSION_REQUEST_CODE)) {
                //Launch file browser app to select PDF file
                openPdfFileBrowser()
            }
        }

private fun askForStoragePermissions(requestCode: Int): Boolean {
    if (!isStoragePermissionsAllowed()) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this.requireActivity(),
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
        ) {
            showPermissionDeniedDialog()
        } else {
            ActivityCompat.requestPermissions(
                this.requireActivity(),
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                requestCode
            )
        }
        return false
    }
    return true
}

Once the permission is granted I browse and select the pdf

but when I try to upload the file I get an exception

java.io.FileNotFoundException: /storage/emulated/0/Download/Gmail - Extension quote.pdf: open failed: EACCES (Permission denied)

private fun uploadFile(sourceFile: File, uploadedFileName: String? = null, model: UtilityBillModel) {
    Thread {
        val mimeType = getMimeType(sourceFile)
        if (mimeType == null) {
            return@Thread
        }
        val fileName: String = uploadedFileName ?: sourceFile.name
        try {
            val requestBody: RequestBody = MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("key", model.fieldsToUseInPost.key)
                .addFormDataPart("acl", model.fieldsToUseInPost.acl)
                .addFormDataPart("Content-Type", model.fieldsToUseInPost.ContentType)
                .addFormDataPart("x-amz-meta-xxx", model.fieldsToUseInPost.XAmzMetaOhmeId)
                .addFormDataPart("X-Amz-Credential", model.fieldsToUseInPost.XAmzCredential)
                .addFormDataPart("X-Amz-Algorithm", model.fieldsToUseInPost.XAmzAlgorithm)
                .addFormDataPart("X-Amz-Date", model.fieldsToUseInPost.XAmzDate)
                .addFormDataPart("Policy", model.fieldsToUseInPost.Policy)
                .addFormDataPart("X-Amz-Signature", model.fieldsToUseInPost.XAmzSignature)
                .addFormDataPart("file", fileName, sourceFile.asRequestBody(mimeType.toMediaTypeOrNull()))
                .build()

            val request: Request = Request.Builder().url(model.s3Host).post(requestBody).build()
            val response: Response = client.newCall(request).execute() // this lines throws the exception

           if (response.isSuccessful) {
               viewModel.onUploadSuccessful()
            } else {
               viewModel.onUploadFailed()
            }
        } catch (ex: Exception) {
            viewModel.onUploadFailed()
        }
    }.start()
}

I ran out of things I could try, is there anything I am missing here please

Thanks for your help in advance R

BRDroid
  • 3,920
  • 8
  • 65
  • 143
  • Use`File.exists()` and `File.canRead()` before you try to upload a file. – blackapps Feb 01 '22 at 13:23
  • `/storage/emulated/0/Download/Gmail` ? What is Gmail? A folder name or a file name? What kind of path are you offering the upload function? – blackapps Feb 01 '22 at 13:25
  • Also, For `MANAGE_EXTERNAL_STORAGE` you've to redirect the user to settings(as it's special permission) & they'll enable a switch from there to allow access. Did you do that? like this: https://stackoverflow.com/a/66366102/9715339 – Mayur Gajra Feb 01 '22 at 13:26
  • @blackapps thanks for your response, Gmail... is the file name sorry for the confusion – BRDroid Feb 01 '22 at 13:36
  • `UseFile.exists() and File.canRead() before you try to upload a file. ` React! Answer! – blackapps Feb 01 '22 at 13:39
  • `Extension quote.pdf:` Please explain where this is coming from. And why dont you give your file a .pdf extension when it is a .pdf? – blackapps Feb 01 '22 at 13:40
  • @blackapps i have tried `sourceFile.exists()` value is `true` and `sourceFile.canRead()` which is `false`. – BRDroid Feb 01 '22 at 13:43
  • I am picking the file from the download folder on the phone `/storage/emulated/0/Download/` – BRDroid Feb 01 '22 at 13:44
  • Wel... than you know why you get that exception. And i have no idea what you do if you pick a file... Pretty unclear. `Once the permission is granted I browse and select the pdf` How? – blackapps Feb 01 '22 at 13:44
  • @blackapps it is a completely different issue now, I am not basically getting the correct uri for the files due to which this is happening. shall I create a new question and tag you ? – BRDroid Feb 01 '22 at 14:58
  • A new question is ok but tagging me is useless as i have no solution. – blackapps Feb 01 '22 at 15:05
  • sure @blackapps thanks – BRDroid Feb 01 '22 at 15:14

0 Answers0