tags:

views:

77

answers:

3

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?

+2  A: 

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
@Thanks it is working
sivaraj
A: 

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
A: 

Hi, I was just wondering if you've gotten this working on android platform 1.5 or 1.6? Attachments initiated in my app and stored in my app's data directory work fine for me on 2.2 and 2.1 but can't seem to get them working in the earlier platform versions. I am setting file mode to MODE_WORLD_READABLE and attaching a pdf that I generate. Any ideas? Thanks!

Works on v2.2 and 2.1:

try {
            FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
            fos.write(pdfContent.getBytes());
            fos.close();
        } catch(IOException e) {
        }


File file = new File(context.getFilesDir().getAbsolutePath() + "/" + fileName);

    Intent i = new Intent(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    i.putExtra(Intent.EXTRA_TEXT, "Message");
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    i.setType("text/plain");
    context.startActivity(Intent.createChooser(i,"Send mail"));
hobgillin