简体   繁体   English

带有HTML的TextView + <img base64/>

[英]TextView with Html + <img base64/>

I would like to display images (png, gif) stored in a string that has the html tag ( , < im /> ...). 我想显示存储在具有html标记(,<im /> ...)的字符串中的图像(png,gif)。 Show Html no problem, but the images aren't working. 显示HTML没问题,但是图像无法正常工作。 I saw examples on the Internet but only for a string that contains only ONE image without text and more. 我在Internet上看到了一些示例,但仅显示了一个字符串,该字符串仅包含一个图像而没有文本等等。

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

Example: 例:

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

Thx all for answers. 感谢所有答案。

In short, to make this happen, you need use the expanded overload of Html.fromHtml: 简而言之,要实现这一点,您需要使用Html.fromHtml的扩展重载:

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

And you have to create the CustomImageGetter itself (it implements ImageGetter), and make CustomImageGetter.getDrawable return a drawable constructed from your byte string. 并且您必须创建CustomImageGetter本身(它实现了ImageGetter),并使CustomImageGetter.getDrawable返回从您的字节字符串构造的可绘制对象。 It might be easier to do a WebView, or to save the byte string as a resource and use the CustomImageGetter to get that instead. 进行WebView或将字节字符串另存为资源并使用CustomImageGetter来获取,可能会更容易。

This question is similar to another stack overflow question: android-TextView setText in Html.fromHtml to display image and text , which can help you set up a CustomImageGetter that will return a drawable from a resource. 这个问题类似于另一个堆栈溢出问题: Html.fromHtml中的android-TextView setText显示图像和文本 ,这可以帮助您设置CustomImageGetter,该CustomImageGetter将从资源中返回可绘制对象。 Then you can start playing around with getting the byte string to work. 然后,您就可以开始使用字节字符串了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM