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/