Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach an image file in email?

Tags:

android

I want to attach an image with email, that image is stored in /data/data/mypacke/file.png. How can I attach that image file programmatically? What would sample code look like?

like image 691
sivaraj Avatar asked Sep 14 '10 14:09

sivaraj


People also ask

How do you send a photo as an attachment?

In the top-right side of the screen, click the Compose new message button. When the new message appears, click the Attach a document to this message button (paper clip). Locate the image you want to send and select Choose File. Input the address of the user that you want to send the message.

How do I insert a JPEG into an email?

Right-click on the photo and choose "Copy." Alternatively, highlight the photo by clicking and dragging the mouse cursor over it and then either press "Ctrl-C" on the keyboard or click "Edit," "Copy." After copying the photo, move to the email message in which you want to embed it.


3 Answers

Use Intent.ACTION_SEND to hand the image off to another program.

File F = new File("/path/to/your/file.png");
Uri U = Uri.fromFile(F);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/png");
i.putExtra(Intent.EXTRA_STREAM, U);
startActivity(Intent.createChooser(i,"Email:"));
like image 157
Blumer Avatar answered Oct 23 '22 00:10

Blumer


I've done exactly what Blumer did and ran into permissions problems unless the file was on the sdcard or unless the file has MODE_WORLD_READABLE access.

like image 41
dhaag23 Avatar answered Oct 23 '22 00:10

dhaag23


Worth noting that if the file is located in internal storage and set to MODE_PRIVATE (which it should be) then you should set the file to be readable by other programs before launching the intent. Using the code from the answer,

File F = new File("/path/to/your/file.png");
F.setReadable(true, false);                     // This allows external program access
Uri U = Uri.fromFile(F);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/png");
i.putExtra(Intent.EXTRA_STREAM, U);
startActivity(Intent.createChooser(i,"Email:"));
like image 2
bkane521 Avatar answered Oct 23 '22 01:10

bkane521