android - 附加到gmail时pdf的大小显示为0

标签 android pdf email-attachments

String emailAddress[] = {""};

    File externalStorage = Environment.getExternalStorageDirectory();

    Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "com.example.pdf/sample.pdf"));

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
    emailIntent.setType("application/pdf");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(emailIntent, "Send email using:"));

logcat 中显示的消息是:

gMail Attachment URI: file:///mnt/sdcard/com.example.pdf/sample.pdf
gMail type: application/pdf
gmail name: sample.pdf
gmail size: 0

问题是样本 pdf 的大小为 0,因此 pdf 没有作为附件在电子邮件中发送。谁能告诉我我做错了什么?

最佳答案

您如何将 pdf 保存到该目录?

如果您使用的是 getExternalStorageDirectory(我假设您是),那么该 pdf 对您的应用程序是私有(private)的,电子邮件应用程序无法访问它。

如果您需要将该文件对您的应用程序保密,并且仍然能够通过电子邮件共享,请使用 ContentProvider : http://developer.android.com/guide/topics/providers/content-provider-creating.html

如果可以将该文件保存在共享目录中,则应使用 getExternalStoragePublicDirectory(): http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

关于android - 附加到gmail时pdf的大小显示为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13015233/

相关文章:

java - 为什么我的 fragment 中的上下文为空?

javascript - 在 GCharts 的 pdf 生成上替代 headless 浏览器

servlets - 禁用 Adob​​e PDF 阅读器中的保存按钮并隐藏 IE 窗口中的菜单栏

android - 如何将 ImageView 半重叠放置在 Fragment 中的 ConstraintLayout 顶部边界上?

java - Firebase 上传监听器永远不会被解雇

android - 为什么会调用fragment的onCreateView、onCreate、onActivityCreated

r - 如何更改 Rmarkdown、LaTeX 和 .pdf 中表格的字体大小?

android - Gmail API 和附件大小

c# - 将 FTP 服务器上现有的文件附加到 C# .NET 中的邮件

JavaMail 将附件作为 base64 添加到 Mime 正文