0

I have something like the following code:

public void shareImageInEmail(String imageUri){
   Intent emailIntent = new Intent(Intent.ACTION_SEND);
   emailIntent.setType("message/rfc822");
   emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   emailIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
   emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri));
   mActivity.startActivity(emailIntent);
}

When the Uri is grabbed from the media folders (camera albums, etc) everything works fine. The problem is when I take a Uri from the assets folder like this:

share("content://com.ex.myapp/logo.png");

In that case, the sharing works but when the e-mail client is opened, the image preview is a grey box, instead of the actual image. When I send the picture is sent correctly, it's just not showing the preview.

Anyone have a solution for this?

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
adrianrdzv
  • 125
  • 1
  • 12

1 Answers1

1

A simple solution will be to copy all contents in Assets to Sdcard and pass 'Sdcard path Uri' as EXTRA_STREAM to Email.

Sample Code:

public void shareImageInEmail(String imageUri){
       Intent emailIntent = new Intent(Intent.ACTION_SEND);        
       emailIntent.setType("message/rfc822");

       emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       emailIntent.putExtra(Intent.EXTRA_TEXT, "Some text");

       Log.v(TAG, "imageUri, file://" + imageUri);
       emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imageUri));
       startActivity(emailIntent);
}

Copy all assets to SDCard (Refer: How to copy files from 'assets' folder to sdcard?)

new File(Environment.getExternalStorageDirectory(), filename); //Store in Sdcard

And finally call shareImageInEmail as follows,

shareImageInEmail(Environment.getExternalStorageDirectory() + "/Image.png");//assets[0]);
Community
  • 1
  • 1
A.Ruppin
  • 161
  • 6
  • Thanks for the response, I marked it as as solved because I used your approach. Basically it was because other packages cannot directly access my package assets. I'm creating a file using AssetManager.open(uri.getPath().substring(1)) and then adding this file as an EXTRA_STREAM in the intent. This solves the problem for my application. – adrianrdzv Jul 11 '13 at 13:32