Ich baue eine PDf mithilfe der PDFtron 庫在 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 wrd 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。
瓦魯姆?und Wie ist die L?sung, damit es funktioniert?
我正在使用 buildPDF() 方法中的 PDFtron 庫構建 PDf。在那里,從 Word 檔案創建 PDF 作為具有各個值的模板。當我在我的應用程式中按下 PDF 按鈕時,Word 模板會從當前視圖的資料從 Android Studio 的 res/raw 檔案夾傳輸到應用程式的內部存盤 transferRawFileToAppFilesDir() ,然后在 buildPDF() 中使用PDFtron 庫 PDF 是從 Word 模板構建的,帶有我的活動視圖的各個值,并存盤在快取檔案夾中。構建 PDF 后,PDF 應該作為電子郵件附件發送,但是出現如下圖所示的錯誤。
為什么?什么是使它起作用的解決方案?
我正在使用此代碼。
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))
}
為什么會發生此錯誤“無法附加檔案”,解決方案是什么?

uj5u.com熱心網友回復:
結果 = Uri.parse("content:/" destinationString)`。
不可以。您不能以這種方式構造內容方案 uri。
您可以洗掉所有 pdf 代碼,因為它與您創建檔案的方式無關。
只需告訴您的應用在某處創建了一個檔案。命名其完整路徑。
然后詢問如何在電子郵件中附加檔案。
答:使用 FileProvider 來提供您的檔案。
FileProvider 將為您提供有效的內容方案 uri。
uj5u.com熱心網友回復:
您需要保存檔案,如下面的示例代碼所示。
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()
}
然后您將需要從“destinationString”構建 URI
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421186.html
標籤:
