Android 应用程序电子邮件附件无法在 Gmail 应用程序中通过

标签 android android-intent android-contentprovider

我的应用创建一个 PDF 文件,将其保存在 context.getFilesDir() 中,并使用内容提供程序传递 URI 来启动电子邮件 Intent。我的实现适用于所有电子邮件应用除了 Gmail。

发生的情况是启动新的电子邮件 Intent 并附加 PDF,名称和大小正确,但发送后,另一端未收到附件。仅电子邮件主题和正文。它在 gmail 中有时确实有效(PDF 可以通过)。大约 90% 的时间似乎都不起作用。

编辑:

我已经尝试了所有可能的存储位置,甚至完全省略内容提供程序并保存到公共(public)文件夹。结果相同。

看来,禁用用于发送电子邮件的帐户的 gmail 同步即可使其正常工作。所以肯定和gmail应用有关。

相关代码部分:

// Creating PDF
PdfDocument document = new PdfDocument();

...

// Writing to file
File file = new File(context.getFilesDir(), mFileName);

try {
    FileOutputStream outputStream = new FileOutputStream(file);
    document.writeTo(outputStream);
    outputStream.close();
} catch (IOException e) {
    e.printSackTrace();
}

Uri uri = FileProvider.getUriForFile(context, "com.redacted.redacted.fileprovider", file);

// Email intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");

// To
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mEmail});

// Subject
intent.putExtra(Intent.EXTRA_SUBJECT, getEmailSubject());

// Attachment
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

// Launch intent
context.startActivity(Intent.createChooser(intent, "Sending PDF..."));

list

...
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.redacted.redacted.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/paths"/>
</provider>
...

xml/paths.xml

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

我相信一切都正确实现,因为它在 Inbox、三星邮件应用程序和其他电子邮件应用程序中都有效。只是 Gmail 似乎当它发送到之前收到过附件的地址时,PDF 无法发送。

知道为什么吗?

最佳答案

您必须设置“mailto”Uri 才能将电子邮件发送到 Gmail 等电子邮件客户端。并且操作将为 ACTION_SENDTO 而不是 ACTION_SEND。

根据您的 Intent 编写这样的代码:

// Email intent
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));

// To    
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mEmail});

// Subject
intent.putExtra(Intent.EXTRA_SUBJECT, getEmailSubject());

// Attachment
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

// Launch intent
context.startActivity(Intent.createChooser(intent, "Sending PDF..."));

关于Android 应用程序电子邮件附件无法在 Gmail 应用程序中通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000633/

相关文章:

Android ContentProvider 检查网络访问

android - S 浏览器书签 URI

android - 自动授予 Uri 权限

android - 破坏方法 Android 的问题

android - Activity 动画在 Galaxy Tab 中不起作用

机器人:首选项

安卓 N : How to launch activity in current active window instead of second window when click on notification in split-screen?

java - Android/Java 试图找出放置变量声明的位置

android - 基于语音(噪音)强度级别的语音识别?

java - 关闭 android 中的 Activity