views:

78

answers:

1

I am trying to send a pdf as an attachment from Android. Here is the code:

String[] mailto = {"[email protected]"};
Uri uri = Uri.parse("android.resource://com.mywebsite.sendemail/raw/mypdf");

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

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

Now this works but the problem is that the attachment is called mypdf instead of mypdf.pdf. I cannot figure out how to send it with it's extension... That's what I need help with. Thanks.

A: 

I am unconvinced what you want will be possible, since you are pulling the PDF from a resource. If you copy the PDF to a local file (with the correct extension) and send that, you should get the extension in the resulting message. But straight out of the resource...I suspect there's no way to add the extension.

CommonsWare
Thanks Mark. I have been able to solve the problem by copying the file to the SDCard and then using Uri.fromFile. I don't like the solution because if the SDCard is missing or busy, it won't work. I put a copy of the pdf in the /assets directory but have not been able to get it to work with Uri.fromFile or Uri.parse. Not sure why...
nickfox
Hmmm...`file:///android_asset/mypdf.pdf` might work. I have only used that syntax for things in my own app, though, not for passing to another app.
CommonsWare
I tried 18 different permutations of fromFile and parse including using android_asset as you suggested. I pretty well have convinced myself that it must be read from the SDCard... Which is unfortunate. That is something that could really be improved in Android... Here is the uri I ended up using to read from the card: Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "mypdf.pdf"));
nickfox