如何在Android中的HTML电子邮件中包含图像

问题描述:

我试图在我的电子邮件正文中使用HTML发送图像,但图像未发送。我不知道我做错了什么。我尝试使用下面的代码:如何在Android中的HTML电子邮件中包含图像

Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE); 
i.setType("text/html"); 
String tempStr = "<html><head><title></title><style> b{ color: red}    </style></head><body><img src=\""+ "images/assaultoffences.png" + "\"><b>quick</b></body> </html>"; 
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(tempStr)); 
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
startActivity(i); 

try { 
    startActivity(Intent.createChooser(i, "Send mail...")); 
} catch (android.content.ActivityNotFoundException ex) { 
    Toast.makeText(SavePage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
} 
+0

是你的图片src的引用是否正确? – Jram 2012-02-23 06:20:31

+0

是的,我的图像是正确的。但在身体只有小符号 – user1083266 2012-02-23 06:28:18

+0

你有13个问题,他们中的大多数有多个答案。如果至少有一个答案满足您的要求,您应该真的接受每个问题的最佳答案。 – kabuko 2012-02-23 07:32:21

图像需要托管的地方。当用户收到电子邮件时,客户端将会查找不存在的图像“images/assaultoffences.png”。 将它托管在某个地方,然后指定电子邮件中的完整URL,例如http://mydomain.com/images/assaultoffences.png

嗨,您可以使用this参考为您的解决方案。

我希望这会帮助你解决你的问题。

谢谢。

仅仅因为HTML引用的图像并不意味着电子邮件应用程序会读取HTML,解析它,找到所引用的图片,将它们连接到电子邮件,并更改引用是内部的。

目前已经是一个question and answer描述了如何发送时他们将文件附加到电子邮件。

This answer展示了如何从HTML参考您的附加的图像,使它们在身体出现(它的工作原理为Outlook至少)。

这令人沮丧的似乎并没有在Android本机支持。一种解决方法是使用Java Mail并自己解决。我所做的是使用droidQuery库,帮助写我HTML到一个文件,并将其附加到电子邮件:

final Intent shareIntent = new Intent(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
String html = "<div>Place Your HTML here.</div>"; 
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html)); 
shareIntent.setType("text/html"); 
$.with(this).write(html, FileLocation.EXTERNAL, "email.html", false, false, new Function() { 

    @Override 
    public void invoke($ droidQuery, Object... params) { 
     String file = ((File) params[1]).getAbsolutePath(); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); 
     startActivity(shareIntent); 
    } 
}, new Function() { 
    @Override 
     public void invoke($ droidQuery, Object... params) { 
      Log.e("$", (String) params[1]); 
     } 
}); 

然后在HTML <body>(也格式混乱的Android上的电子邮件)的顶部添加了此块:

<div style="background: gray; color: black; align='center'" > 
    Trouble viewing this email? Open the attachment to view in your web browser. 
</div> 

如果你正在考虑的第一种方法(Java邮件),我会建议使用droidMail扩展droidQuery,使用该库提供一个简化的API。