Ich baue eine PDf mithilfe der PDFtron library in der buildPDF() Methode. Dort wird aus einer Word Datei als Vorlage eine PDF mit individuellen Werten erzeugt. Wenn ich in meiner App auf den PDF Button drücke, wird aus den Daten der aktuellen Ansicht die Word Vorlage vom res/raw folder von Android Studio in den Internal Storage der App transferiert transferRawFileToAppFilesDir() und danach wird in der buildPDF() mithilfe der PDFtron library eine PDF aus der Word Vorlage mit den individuellen Werten von meine Activity-Ansicht gebaut und wird im Cache Ordner gespeichert. Nachdem die PDF gebaut wurde, soll die PDF als E-Mail Anhang verschickt werden, allerdings kommt ein Fehler, der unten im Bild angezeigt ist.
Warum? und Wie ist die Lösung, damit es funktioniert?
I am building a PDf using the PDFtron library in the buildPDF() method. There a PDF is created from a Word file as a template with individual values. When I press the PDF button in my app, the Word template is transferred from the data of the current view from the res/raw folder of Android Studio to the internal storage of the app transferRawFileToAppFilesDir() and then in the buildPDF() using the PDFtron library a PDF is built from the Word template with the individual values of my activity view and is stored in the cache folder. After the PDF is built, the PDF is supposed to be sent as an email attachment, however, an error comes up which is shown in the image below.
Why? and what is the solution to make it work?
I'm Using this Code.
private fun buildPDF(jsonObject: JSONObject): Uri {
var result: Uri
val doc = PDFDoc()
val options = OfficeToPDFOptions()
options.templateParamsJson = jsonObject.toString()
val rawSourceString = this.filesDir.absolutePath "/template_display.docx"
val destinationString = this.cacheDir.absolutePath "/display.pdf"
Log.d(TAG, "rawSourceString: " rawSourceString)
Log.d(TAG, "destinationString: " destinationString)
transferRawFileToAppFilesDir(R.raw.template_displayansicht)
try {
Convert.officeToPdf(doc, rawSourceString, options)
val outStream = FileOutputStream(destinationString)
doc.save(outStream, SDFDoc.SaveMode.NO_FLAGS, null)
Log.d(TAG, "officeToPdf(): erfolgreich")
result = Uri.parse("content:/" destinationString)
Log.d(TAG, "URI: " result.toString())
} catch (e: Exception) {
e.printStackTrace()
Log.e(TAG, "" e.message)
result = Uri.fromFile(File("Test"))
}
return result
}
private fun transferRawFileToAppFilesDir(resId: Int) {
val inputStream = resources.openRawResource(resId)
val outputStream = FileOutputStream(this.filesDir.absolutePath "/template_display.docx")
val buffer = ByteArray(1024)
var length = inputStream.read(buffer)
while (length > 0) {
outputStream.write(buffer, 0, length)
length = inputStream.read(buffer)
}
inputStream.close()
outputStream.close()
}
fun composeMail(activity: Activity, subject: String, text: Spanned?, attachement: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
data = Uri.parse("mailto:")
type = "*/*"
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, text)
putExtra(Intent.EXTRA_STREAM, attachement)
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(Intent.createChooser(intent, null))
}
Why this error "Coulnd't Attach File" happends and what is the solution?
CodePudding user response:
result = Uri.parse("content:/" destinationString)`.
No. You cannot construct a content scheme uri in this way.
You can remove all pdf code as it is irrelevant how you created a file.
Just tell that your app created a file somewhere. Name its full path.
Then ask how to attach a file on an email.
Answer: Use FileProvider to serve your file.
FileProvider will give you a valid content scheme uri.
CodePudding user response:
You need to save the file as shown in the sample code below.
val doc = PDFDoc()
val options = OfficeToPDFOptions()
options.templateParamsJson = jsonObject.toString()
val rawSourceString = this.filesDir.absolutePath "/template_display.docx"
val destinationString = this.cacheDir.absolutePath "/display.pdf"
Log.d(TAG, "rawSourceString: " rawSourceString)
Log.d(TAG, "destinationString: " destinationString)
try {
Convert.officeToPdf(doc, rawSourceString, options)
doc.save(destinationString, SDFDoc.SaveMode.NO_FLAGS, null)
} catch (e: Exception) {
e.printStackTrace()
}
Then you will need to build the URI from the "destinationString"

