TextView with Html + <img base64=""/>

时间:2015-05-22 10:12:23

标签: android html image textview

我想显示存储在具有html标记(&lt; im /&gt; ...)的字符串中的图像(png,gif)。 显示Html没问题,但图像不起作用。 我在互联网上看到了一些例子,但只看到一个只包含一个没有文字的图像的字符串。

description.setText(Html.fromHtml(new.getDescription()));

示例:

"<p>Text before image</p>\r\n<p><img style="max-width: 100%;" src="data:image/gif;base64,R0lGODlhMgA2APQAACgoKDg4ODiQaFCQEHi4IEDYcGiIuIhYKLhoIMAYIPh/></p>"

谢谢所有答案。

1 个答案:

答案 0 :(得分:-1)

简而言之,要实现这一点,您需要使用Html.fromHtml的扩展重载:

description.setText(Html.fromHtml(new.getDescription(), new CustomImageGetter(), null));

您必须创建CustomImageGetter本身(它实现ImageGetter),并使CustomImageGetter.getDrawable返回从您的字节字符串构造的drawable。可能更容易做WebView,或者将字节字符串保存为资源,并使用CustomImageGetter来获取它。

这个问题类似于另一个堆栈溢出问题:android-TextView setText in Html.fromHtml to display image and text,它可以帮助您设置一个CustomImageGetter,它将从资源返回一个drawable。然后你就可以开始使用字节串了。