提问人:Hugo Jiménez 提问时间:10/18/2023 最后编辑:Hugo Jiménez 更新时间:10/18/2023 访问量:66
使用 Kotlin 在 Android 中发送带有文件附件的电子邮件
send an email with a file attachment in Android using Kotlin
问:
我正在尝试发送一封电子邮件,其中包含应用程序生成并保存在手机文档文件夹 (/storage/emulated/0/Documents/) 中的 pdf,我的代码没有给出错误,但是当应用程序打开 Gmail 时,我收到一个带有 gmail 徽标的吐司,说无法附加文件。
在 pdf() 函数中,我创建了从 send() 函数获取的 pdf 文件,这是我处理电子邮件主题、电子邮件文本和收件人电子邮件的所有意图的地方
private fun envio(nombre :String) {
val direcciones = arrayOf(binding.etEnvio.text.toString())
val asunto = "Prueba intent implícito"
val texto = "Este es un mensaje de prueba que queremos enviar"
Log.d("ENVIO", "intento enviar el pdf")
// Ruta del archivo PDF que has guardado
val pdfFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "$nombre.pdf")
val pdfPath = pdfFile.toString().trim()
Log.d("ENVIO", "RUTA: $pdfPath")
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:")
intent.putExtra(Intent.EXTRA_EMAIL, direcciones)
intent.putExtra(Intent.EXTRA_SUBJECT, asunto)
intent.putExtra(Intent.EXTRA_TEXT, texto)
// Adjuntar el archivo PDF
val pdfUri = FileProvider.getUriForFile(this, "com.example.appcamara.fileprovider", File(pdfPath))
intent.putExtra(Intent.EXTRA_STREAM, pdfUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(intent, "Elige cliente de correo:"))
}
在我的 AndroidManifest 中,我有以下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.appcamara.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
我为我的添加了这条路径:File_Provider
<external-files-path name="documents" path="Documents/" />
仍然不起作用
答:
1赞
blackapps
10/18/2023
#1
答案就在评论中。
虽然不清楚到底有什么帮助。
评论
0赞
Hugo Jiménez
10/19/2023
更改为 和 更改为val intent = Intent(Intent.ACTION_SENDTO)
val intent = Intent(Intent.ACTION_SEND)
intent.data = Uri.parse("mailto:")
intent.type = "application/pdf"
评论
when the app opens Gmail
用户也可以选择其他应用程序。他们怎么说?And where i can add the intent.ACTION_SEND
您不会添加,而是使用它而不是ACTION_SENDTO。