6

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sivaraj
  • 1,849
  • 9
  • 35
  • 52

3 Answers3

25

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:"));
Blumer
  • 5,005
  • 2
  • 33
  • 47
3

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.

dhaag23
  • 6,106
  • 37
  • 35
2

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:"));
bkane521
  • 386
  • 2
  • 10