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是您应用程序的私有,并且电子邮件应用程序无法访问。
如果您需要将该文件保密到您的应用程序中,并且仍然可以通过电子邮件共享,请使用ContentProvider
:http://developer.android.com/guide/topics/providers/content-provider-creating.html
如果您可以将该文件保存在共享目录上,则应使用getExternalStoragePublicDirectory()
:http://developer.android.com/guide/topics/data/data-storage.html#filesexternal