base64图像和html.fromhtml android [英] base64 image and html.fromhtml android
问题描述
我的数据以html格式存储在数据库中.
My data are stored in html format on db.
图像以base64格式存储(作为db中的字符串).
The image are stored in base64 format (as a string in db).
我正在尝试显示此数据.
I am trying to show this data.
我的textView设置如下:
My textView is setted like this:
setText(Html.fromHtml(content));
setText(Html.fromHtml(content));
内容"中的所有html标记均正确显示.除了'img'标签(其中包含base64编码图像).
all html tags in 'content' are being displayed correctly. Except 'img' tag (which contains a base64 encode image).
所以,我的问题是: 'Html.fromHtml'的标签'img'可以解码具有base64图像的字符串吗?
So, my question is: the tag 'img' of 'Html.fromHtml' can decode a string with base64 image?
p.s:标记所在的位置仅显示一个灰色小方块.没有错误.
p.s: The place where the tag is show just a little gray square. No got errors.
thx.
推荐答案
结合使用Html.fromHtml
和您自己的Html.ImageGetter
实现.
Use Html.fromHtml
combined with your own implementation of Html.ImageGetter
.
When overriding Html.ImageGetter.getDrawable
, convert the Base64 string into a byte array (you can use android.util.Base64
) and feed it into BitmapFactory.decodeByteArray
to produce a Bitmap
which you can then pass into the constructor of a BitmapDrawable
to return.
例如:
Html.fromHtml(content, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
byte[] data = Base64.decode(source, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return new BitmapDrawable(getResources(), bitmap);
}
}, null);
这篇关于base64图像和html.fromhtml android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!