How to attach image file via email?

I want to add an image by email, this image is saved in /data/data/mypacke/file.png . How can I attach this image file programmatically? What will be the sample code?

+6
android
Sep 14 '10 at 14:13
source share
3 answers

Use Intent.ACTION_SEND to transfer the image 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:")); 
+25
Sep 14 '10 at 15:57
source share

I did exactly what Blumer did and ran into permission problems if the file was not on the SD card or if the file does not have MODE_WORLD_READABLE access.

+3
Sep 14 '10 at 16:51
source share

It is worth noting that if the file is located in the internal storage and set to MODE_PRIVATE (what it should be), you must 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:")); 
+2
Apr 25 '14 at 17:21
source share



All Articles