android - 使用 FileProvider 发送带有附件的电子邮件

标签 android file mobile

我尝试通过 gmail 发送带有附件的邮件,代码如下:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3347564047735e525a5f1d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>", null));

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "test Mail");
File file = ..function that return file with logs...


File newFile = new File(getFilesDir() + File.separator + "logs" + File.separator + System.currentTimeMillis() + ".log");

FileTools.fileCopy(file, newFile);

Uri uri = FileProvider.getUriForFile(this,
            "test.test.test.provider", newFile);

emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivityForResult(Intent.createChooser(emailIntent, "eMail"), 1);

我还添加了文件夹日志的路径

<files-path  name="logs" path="logs/" />

它运行 gmail 应用程序,并显示“无法附加文件”。 在调试 uri 变量中看起来:

content://test.test.test.provider/logs/1545415598572.log

当我改变

Uri uri = FileProvider.getUriForFile(this, "test.test.test.provider", newFile);

Uri uri = Uri.uri(newFile);

它可以工作,但只能处理外部存储中的文件。

为什么它不能与 FileProvider 一起使用?

最佳答案

应用程序包 - ru.exampleapp

file_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="." />
</paths>

然后在 Android list 中添加提供程序。

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="ru.exampleapp"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider" />
</provider>

这是一个示例,说明如何创建和启动 Intent 。

val emailIntent = Intent(Intent.ACTION_SENDTO)
val intent = Intent(Intent.ACTION_SEND)
val devInfoFile = context.getFileStreamPath("debug_info.txt")
val outputWriter = OutputStreamWriter(devInfoFile.outputStream())
outputWriter.write("Model: ${Build.MODEL}\n")
outputWriter.write("Id: ${Build.ID}\n")
outputWriter.write("Manufacturer: ${Build.MANUFACTURER}\n")
var devInfoUri: Uri? = null
try {
    devInfoUri = FileProvider.getUriForFile(context, "ru.exampleapp", devInfoFile)
} catch (e: java.lang.Exception) { }
intent.putExtra(Intent.EXTRA_STREAM, devInfoUri)
emailIntent.data = Uri.parse("mailto:")
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(context.getString(R.string.admin_email)))
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.selector = emailIntent
try {
    context.startActivity(Intent.createChooser(intent, 
    context.getString(R.string.report_message)))
} catch (e: java.lang.Exception) {
}

我还添加了选择器,这是仅通过电子邮件应用程序发送的方式。

关于android - 使用 FileProvider 发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53889141/

相关文章:

java - 如何在不破坏 CoordinatorLayout.LayoutParams 的情况下重写 CoordinatorLayout.Behavior

android - 如何将基本适配器更改为Recycler View适配器?

php - 如何安全上传和存储用户文档?

javascript - 移动可视区域的高度(在地址栏和导航栏之间)

javascript - html 表单值在移动浏览器上触摸后重置

android - BitmapFactory 无法解码流。致命的空指针错误

java - 从android上传文件到服务器失败

php - 如何使用 PHP 加密和解密 PDF?

c++ - 最优锁文件方法

javascript - 确定浏览器是否正在使用移动数据或 wifi?