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