在android中使用
我已经阅读了很多关于从 android 发送 html 电子邮件的限制的内容。所有发送 html 电子邮件的建议似乎只是将 Html.fromHtml(yourHtmlString) 作为 Intent.EXTRA_TEXT 传递给意图。这适用于一些基本标签 - 粗体、斜体 - 但不适用于 html 表格之类的内容。
看起来你可以尝试扩展 Html 的一些功能或实现你自己的标记处理程序,但我想知道是否没有更基本的限制会迫使你做一些完全不同的事情(比如使用邮件 api 或其他东西) )。
我建议这样做的原因是,就意图本身而言, Html.fromHtml(blah) 只是一个 charsequence,如果您在该对象上调用 charsequence 接口上的方法,您将看不到任何 html 内容(在至少我没有)。所有的 html/tag 内容似乎都包含在 Html.fromHtml 实际上返回的 SpannableStringBuilder 中......我想知道 gmail 应用程序是否在幕后查看字符序列到底是什么,然后可以处理一些标签,这意味着在您的应用程序方面没有希望做任何事情来让 gmail 应用程序处理比粗体、斜体等更复杂的内容。
我实际上已经查看了 gmail 应用程序的原始电子邮件发送,它会自动发送不带标签的 text/plain 和带有有限数量标签的 text/html 版本。我什至尝试插入一些转义的 html 标签,这些标签最终可能会转换为电子邮件的 text/html 部分中的实际标签,但可惜的是,它们仍然是转义的......这当然有点 hacky。
无论如何,对于任何可能对此进行更多研究的人,我想做一个额外的确认,即默认的 android“发送 html 电子邮件”功能将使您非常接近您可能需要的功能,但最终您必须咬住项目符号并自己实现许多较低级别的东西(例如 使用 JavaMail API 在 Android 中发送电子邮件,无需使用默认/内置应用程序,这意味着你必须处理 pw 的东西,等等)。
注意(稍后): 我使用扩展 SpannableStringBuilder 的自定义类包装了从 Html.fromHtml 返回的 SpannableStringBuilder,并将其传递给意图以侦听对 Spanned 接口的调用。事实证明,当将内容写入发送到电子邮件意图的包裹时,TextUtils.writeToParcel 会进行一些特殊检查,通过首先检查 CharSequence 是否是 Spanned 的实例,然后询问是否有粗体/斜体内容。跨度(通过 spanned.getSpans)。尽管如此,我认为没有明显的希望进行修改以得到像在那里处理表/td标签那样简单的东西。我什至尝试修改 SpannableStringBuilder 子类的 toString() 以返回一些原始表 html 以查看会发生什么,但它在包裹写入过程中的其他地方被转义。
还有更多(稍后): TextUtils.writeToParcel(CharSequence cs, Parcel p,...) 如果 cs 是“Spanned”的实例,则仅当它们实现“ParcelableSpan”接口时才写入这些跨度...这是“一种特殊的 Parcelable for将用作文本跨度的对象”并且“只能由框架中的代码使用;它不适合应用程序实现自己的可打包跨度”。因此,即使您想加入其中并编写自己的来处理表标签或其他内容,似乎也不鼓励。伙计,我希望 hackbod 能在这里发表一些我错过的明显的事情。
I have read a good bit on the limitations of sending html email from android. All suggestions to send html email seem to be to just pass Html.fromHtml(yourHtmlString) to the intent as Intent.EXTRA_TEXT. This works for a few basic tags - bold, italic - but won't for anything like an html table.
It looks like you could try to extend some of the functionality of either Html or implement your own taghandler, but I am wondering if there is not a more fundamental limitation that will force you to do something completely different (like with the mail api or something).
The reason I suggest this is because, as far as the intent itself knows, Html.fromHtml(blah) is simply a charsequence, and if you call the methods on the charsequence interface on this object you don't see any html stuff (at least I didn't). All of the html/tag stuff seems to be wrapped up in the SpannableStringBuilder that Html.fromHtml actually returns... and I am wondering if the gmail app looks under the covers to see what the charsequence really is and then can handle a few tags, which means that there is no hope in doing anything on your app's side of things to get/trick the gmail app to handle anything more complicated than bold, italic, etc.
I have looked at the raw email the gmail app actually sends, and it automatically sends both a text/plain with no tags, and the text/html version with the limited number of tags. I even tried sticking in some escaped html tags that might ultimately get converted to actual tags in the text/html part of the email, but alas they stayed escaped... and that would of course be a bit hacky.
Anyway, for anyone who might have looked into this more, I wanted to do an additional confirmation that the default android "send html email" functionality will get you maddeningly close to what you might need, but in the end you've got to bite the bullet and implement a lot of lower level stuff yourself (such as Sending Email in Android using JavaMail API without using the default/built-in app , which means you've got to deal with the pw stuff, etc.).
Note (later):
I wrapped the SpannableStringBuilder returned from Html.fromHtml with a custom class that extended SpannableStringBuilder and passed that to the intent to listen for calls to the Spanned interface. It turns out that when things are written to the parcel that is sent to the email intent, TextUtils.writeToParcel does some special checking to root out the bold/italic stuff by first checking if the CharSequence is an instance of Spanned, and then asking for the spans (via spanned.getSpans). Nevertheless, I see no obvious hope in making the modifications to get something as simple as table/td tags handled in there. And I even tried modifying the toString() of my subclass of SpannableStringBuilder to return some raw table html to see what would happen, but it gets escaped somewhere else down there in the parcel-writing process.
And More (Later):
TextUtils.writeToParcel(CharSequence cs, Parcel p,...) will, if cs is an instance of "Spanned", write those spans only if they implement the "ParcelableSpan" interface... which is "A special kind of Parcelable for objects that will serve as text spans" and "can only be used by code in the framework; it is not intended for applications to implement their own Parcelable spans". So, even if you wanted to hook into this and write your own to handle table tags or whatever, it seems to be discouraged. Man I wish hackbod would weigh in here with something obvious I've missed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是电子邮件客户端的功能。并非所有电子邮件客户端都可以在任何平台上编写任意 HTML。因此,虽然 Mozilla Thunderbird 似乎允许您使用表格创建 HTML 邮件,但 Gmail 却不允许(至少,我在邮件撰写窗口中没有看到它的选项)。
除非您编写自己的电子邮件客户端,扩展允许
TextView
和EditText 来处理 HTML 表格(它方式不仅仅是
Html
类)对您没有任何好处。TextView
和EditText
可以“处理一些标签” ,大致与Html
可以解析/生成的内容和SpannedString
可以表示的内容一致。这些都不能处理 HTML 表格。也不是 JavaScript。也不是CSS。也不是
iframe
或任意数量的其他标记。我首先要问自己,直接从手机发送带有表格的 HTML 邮件是否值得。您可以使用 Web 服务界面从服务器发送带有表格的 HTML 邮件,也可以从电话发送不带表格的 HTML 邮件。这些都不需要你收集“私交的东西”。
That is a function of the email client, most likely. Not all email clients can author arbitrary HTML, on any platform. So, while Mozilla Thunderbird appears to let you create an HTML mail with a table, Gmail does not (leastways, I don't see an option for it in the message-compose window).
Unless you write your own email client, extending the several classes needed to allow
TextView
andEditText
to handle HTML tables (it's way more than just theHtml
class) will do you no good.TextView
andEditText
can "handle a few tags", lining up roughly with whatHtml
can parse/generate andSpannedString
can represent.None of that can handle an HTML table. Nor JavaScript. Nor CSS. Nor
iframe
or any number of other tags.I'd start by asking yourself whether sending HTML mail with tables from the phone directly is worth it. You could send HTML mail with tables from your server using a Web service interface, or you could send HTML mail sans tables from the phone. Neither of those would require you to collect "the pw stuff".