5

I'm trying to send email from my application with it's logo.
But I receive the email when the attachment in string format(should be png).
My code:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("application/image");

    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.fb_share_description));
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://my.package/" + R.drawable.ic_launcher));
   Intent chooser = Intent.createChooser(intent, "share");
   startActivity(chooser);

What should I do?

NickF
  • 5,637
  • 12
  • 44
  • 75

1 Answers1

8

You cannot attach files to an email from your internal resources. You must copy it to a commonly accessible area of the storage like the SD Card first.

InputStream in = null;
OutputStream out = null;
try {
    in = getResources().openRawResource(R.drawable.ic_launcher);
    out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png"));
    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
} catch (Exception e) {
    Log.e("tag", e.getMessage());
    e.printStackTrace();
}


private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

//Send the file
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

This is required as the resources you bundle with your app are read only and sandboxed to your application. The URI that the email client receives is one it cannot access.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Can I keep the file in assets? – NickF Aug 19 '13 at 10:35
  • +1 well explained...@Raghav Sood Can we use content Providers for this? – Arun C Aug 19 '13 at 10:35
  • @NickF You can keep the file wherever you want, as long as you copy it to the external storage before attaching it. – Raghav Sood Aug 19 '13 at 10:38
  • @TCA That really depends on the email app the user uses. If you make a content provider and the app has no support for them, you're out of luck. Copying to the external storage is a more guaranteed approach. – Raghav Sood Aug 19 '13 at 10:39
  • @Raghav I ment if I can load it from assets directly to email? – NickF Aug 19 '13 at 10:40
  • @NickF Nope. Even your assets are read only and restricted to your app. The email app cannot read anything you bundle with your apk unless you copy it to a shared storage area like the external storage. – Raghav Sood Aug 19 '13 at 10:41
  • You can make the assets attached to your app world readable like so: FileOutputStream fos = context.openFileOutput(path, Context.MODE_WORLD_READABLE). You can then share them with other apps easily. As long as you don't share sensitive data this way, you should be fine – HarshMarshmallow Mar 11 '15 at 20:24