繁体   English   中英

无法将附件添加到 email(Android、Kotlin)

[英]Unable to add attachment to email (Android, Kotlin)

我的应用程序中有一个执行以下代码的导出按钮。 email 应用程序可以正确打开主题等,但缺少附件。 我的执着心好像起不来。 知道我做错了什么吗?

    val filename = "Export.csv"
    val path = context?.getExternalFilesDir(null) //get file directory for this package

    //create fileOut object
    val fileOut = File(path, filename)

    //delete any file object with path and filename that already exists
    fileOut.delete()

    //create a new file
    fileOut.createNewFile()

    //append the header and a newline
    fileOut.appendText("Case Number,Form Type,Status,Last Updated Date")
    fileOut.appendText("\n")

    for (item in arrayAll) {
        fileOut.appendText("${item.number},${item.formType},${item.status},${item.LUD}")
        fileOut.appendText("\n")
    }

val contentUri = FileProvider.getUriForFile(this.requireContext(),"${BuildConfig.APPLICATION_ID}.fileProvider", fileOut)

    val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
        data = Uri.parse("mailto:") // only email apps should handle this
        putExtra(Intent.EXTRA_SUBJECT, "Export.csv")
        putExtra(Intent.EXTRA_STREAM, contentUri)
        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    }

    this.startActivity(emailIntent)

显现:

<provider
        android:authorities="${applicationId}.fileProvider"
        android:name="androidx.core.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>

供应商路径:

<?xml version="1.0" encoding="utf-8"?>

<paths>

    <root-path name="root" path="." />

</paths>

根据您的最新评论; 根据Android 文档Intent.ACTION_SENDTO 用于当您不想要附件时,我认为您需要更改此行val emailIntent = Intent(Intent.ACTION_SENDTO).apply { to val emailIntent = Intent(Intent.ACTION_SEND).apply {让你的 email 应用显示附件

暂无
暂无

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

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