How to show an image in the email body?

Sankar Ganesh PMP picture Sankar Ganesh PMP · Jun 1, 2011 · Viewed 14.7k times · Source

Note: I don't want to attach the Image to the Email

I want to show an image in the email body,

I had tried the HTML image tag <img src=\"http://url/to/the/image.jpg\">" and I got output as you can see in this my question on How to add an image in email body, so I tired Html.ImageGetter.

It does not work for me, it also gives me the same output, so I have a doubt is it possible to do this,

My code

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL,new String[] {"[email protected]"}); 
i.putExtra(Intent.EXTRA_TEXT,
    Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>",
    imgGetter,
    null));

i.setType("image/png");
startActivity(Intent.createChooser(i,"Email:"));


private ImageGetter imgGetter = new ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable = null;
            try {
                drawable = getResources().getDrawable(R.drawable.icon);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("Exception thrown",e.getMessage());
            } 
            return drawable;
    }
};

UPDATE 1: If I use the ImageGetter code for TextView I am able to get the text and image but I am not able to see the image in the email body

Here is my code:

TextView t = null;
t = (TextView)findViewById(R.id.textviewdemo);
t.setText(Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>",
    imgGetter,
    null));

UPDATE 2: I had used bold tag and anchor tag as i shown below these tag are working fine , but when i used img tag i can able to see a square box which say as OBJ

 i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml("<b>Hi</b><a href='http://www.google.com/'>Link</a> <img src='http://url/to/the/image.jpg'>",
        imgGetter,
        null));

Answer

rochdev picture rochdev · Jun 25, 2011

Unfortunately, it's not possible to do this with Intents.

The reason why for example bold text is displayed in the EditText and not an Image is that StyleSplan is implementing Parcelable whereas ImageSpan does not. So when the Intent.EXTRA_TEXT is retrieved in the new Activity the ImageSpan will fail to unparcel and therefor not be part of the style appended to the EditText.

Using other methods where you don't pass the data with the Intent is unfortunately not possible here as you're not in control of the receiving Activity.