In my Manifest.xml the provider looks like this:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
My provider_paths looks like this:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
<cache-path name="cache" path="/" />
</paths>
My pdfGenerator code:
PdfGenerator.getBuilder()
.setContext(this)
.fromViewSource()
.fromView(binding.root)
.setFileName(patId)
.setFolderNameOrPath(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absolutePath)
.actionAfterPDFGeneration(PdfGenerator.ActionAfterPDFGeneration.NONE)
.savePDFSharedStorage(xmlToPDFLifecycleObserver)
.build(object : PdfGeneratorListener() {
override fun onFailure(failureResponse: FailureResponse) {
super.onFailure(failureResponse)
Log.d("termsResp", failureResponse.errorMessage)
}
override fun onStartPDFGeneration() {
}
override fun onFinishPDFGeneration() {
binding.btnUpdateSignature.visibility = View.VISIBLE
binding.btnGeneratePdf.visibility = View.VISIBLE
}
override fun showLog(log: String) {
super.showLog(log)
Log.d("termsResp", log)
}
override fun onSuccess(response: SuccessResponse) {
super.onSuccess(response)
val pdfFile = response.file
val pdfUri = FileProvider.getUriForFile(
this@TermsOfRespActivity,
"${binding.root.context.packageName}.provider",
pdfFile
)
Log.d("termsResp", "$response -- $pdfFile -- $pdfUri -- ${response.pdfDocument}")
uploadTerms(pdfUri)
}
})
My uploadTerms code:
private fun uploadTerms(termsPdf: Uri) {
try {
val userId = mAuth.currentUser!!.uid
val imagesRef = storage.reference
.child("users")
.child(userId)
.child("patients")
.child(patId)
.child("terms")
val dbRef = db
.collection("users")
.document(userId)
.collection("patients")
.document(patId)
val fileName = "terms-$patId.pdf"
val imageRef = imagesRef.child(fileName)
val inputStream = contentResolver.openInputStream(termsPdf)
if (inputStream != null) {
imageRef.putStream(inputStream)
.addOnFailureListener { error ->
Log.d("termsResp", "Erro ao fazer o upload: ${error.message} -- ${error.stackTrace}")
}
.addOnCompleteListener {
imageRef.downloadUrl.addOnSuccessListener { url ->
dbRef
.update("terms", url.toString())
.addOnSuccessListener {
Utils.sendSnackBar(
binding.root,
getString(R.string.updated_successfully)
)
dialog(
getString(R.string.menu_account),
getString(R.string.updated_successfully)
)
finish()
}
.addOnFailureListener { exception ->
dialog(
getString(R.string.error),
exception.message.toString()
)
}
}
}
inputStream.close()
} else {
Log.d("termsResp", "InputStream is null")
}
} catch (exception: Exception) {
Log.d("termsResp", exception.message.toString())
}
}
I am using the Android-XML-to-PDF-Generator to generate pdf files of an entire view in my kotlin app and then I want to save it in my firebase storage, the pdf is generated and saved in firebase correctly only up to api 32, from api 33 onwards pdf is generated but the app is unable to save the file in firebase, the following error appears: open failed: EACCES (Permission denied)
I tried using android:requestLegacyExternalStorage=”true” in Manifest.xml but found that it only works up to Android 10
I tried the MANAGE_EXTERNAL_STORAGE permission, but the app was not accepted