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));
Step 1: Click Settings. Step 2: Click “View all Outlook settings.” Step 3: Click Mail > Layout. Step 4: Under the Sender image section, select “Show sender images.”
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.
Two simple suggestions first:
<img src="..." />
instead of <img src="...">
)i.setType("text/html")
instead of i.setType("image/png")
If neither of those work, maybe you try attaching the image to the email and then referencing it using "cid:ATTACHED_IMAGE_CONTENT_ID"
rather than an "http:URL_TO_IMAGE"
?
Intent i = new Intent(Intent.ACTION_SEND); i.putExtra(Intent.EXTRA_EMAIL,new String[] {"[email protected]"}); i.putExtra(Intent.EXTRA_STREAM, Uri.parse("http://url/to/the/image.jpg"); i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("Hi <img src='cid:image.jpg' />", //completely guessing on 'image.jpg' here imgGetter, null)); i.setType("image/png");
See the section titled Sending HTML formatted email with embedded images in the apache email user guide
Though, then you would need to know the content-id of the attached image, and I'm not sure if that is surfaced via the standard Intent approach. Maybe you could inspect the raw email and determine their naming conventions?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With