[英]Unable to add attachment to email (Android, Kotlin)
I have an export button in my app that executes the below code.我的应用程序中有一个执行以下代码的导出按钮。 The email app opens correctly with the subject etc., but it's missing the attachment. email 应用程序可以正确打开主题等,但缺少附件。 I don't seem to be able to get the attachment to work.我的执着心好像起不来。 Any ideas what I'm doing wrong?知道我做错了什么吗?
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)
Manifest:显现:
<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>
provider_paths:供应商路径:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path name="root" path="." />
</paths>
Following from you latest comment;根据您的最新评论; According to the Android docs Intent.ACTION_SENDTO is for when you don't want attachments, I think you need to change this line val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
to val emailIntent = Intent(Intent.ACTION_SEND).apply {
to get your email app to display the attachment根据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.