问题
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:"));
The message shown in logcat is:
gMail Attachment URI: file:///mnt/sdcard/com.example.pdf/sample.pdf
gMail type: application/pdf
gmail name: sample.pdf
gmail size: 0
The problem is that the size of sample pdf is 0, so the pdf is not sent as an attachment in the email. Can anyone tell what am I doing wrong?
回答1:
How are you saving the pdf on that directory?
If you are using the getExternalStorageDirectory
(i'm assuming you are), that pdf is private to your application, and can't be accessed by the email app.
If you need to keep that file private to your application, and still be able to share via email, use a ContentProvider
:
http://developer.android.com/guide/topics/providers/content-provider-creating.html
If you can keep that file on a shared directory, you should use getExternalStoragePublicDirectory()
:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
来源:https://stackoverflow.com/questions/13015233/the-size-of-pdf-is-shown-0-when-attached-to-gmail