简体   繁体   English

Android Kotlin PDFtron:如何将 pdf 从内部存储连接到 ZCE8AE9DA5B7CD6C3DF29295243。 为什么我的错误“无法附加文件”。 发生了什么?

[英]Android Kotlin PDFtron: How to attach an pdf from internal Storage to an Email. Why my Error "Couldn't attach file." happends?

Ich baue eine PDf mithilfe der PDFtron library in der buildPDF() Methode. Ich baue eine PDf mithilfe der PDFtron 库在 der buildPDF() 方法中。 Dort wird aus einer Word Datei als Vorlage eine PDF mit individuellen Werten erzeugt. 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. 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. 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? und Wie ist die Lösung, damit es funktioniert?

I am building a PDf using the PDFtron library in the buildPDF() method.我正在使用 buildPDF() 方法中的 PDFtron 库构建 PDf。 There a PDF is created from a Word file as a template with individual values. PDF 是从 Word 文件创建的,作为具有单独值的模板。 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.当我在我的应用程序中按下 PDF 按钮时,Word 模板从当前视图的数据从 Android Studio 的 res/raw 文件夹传输到应用程序的内部存储 transferRawFileToAppFilesDir() 然后在 buildPDF() 中使用PDFtron 库 PDF 是从 Word 模板构建的,其中包含我的活动视图的各个值,并存储在缓存文件夹中。 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.在构建 PDF 之后,PDF 应该作为 email 附件发送,但是出现如下图所示的错误。

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?为什么会发生此错误“无法附加文件”,解决方案是什么?

在此处输入图像描述

result = Uri.parse("content:/" + destinationString)`.结果 = Uri.parse("content:/" + destinationString)`。

No. You cannot construct a content scheme uri in this way.不可以。您不能以这种方式构造内容方案 uri。

You can remove all pdf code as it is irrelevant how you created a file.您可以删除所有 pdf 代码,因为它与您创建文件的方式无关。

Just tell that your app created a file somewhere.只需告诉您的应用在某处创建了一个文件。 Name its full path.命名其完整路径。

Then ask how to attach a file on an email.然后询问如何在 email 上附加文件。

Answer: Use FileProvider to serve your file.答:使用 FileProvider 来提供您的文件。

FileProvider will give you a valid content scheme uri. FileProvider 将为您提供有效的内容方案 uri。

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"然后您将需要从“destinationString”构建 URI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM