base64图像和html.fromhtml android [英] base64 image and html.fromhtml android

查看:184
本文介绍了base64图像和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.

请参见重载Html.ImageGetter.getDrawable时,将Base64字符串转换为字节数组(可以使用android.util.Base64),并将其馈入BitmapFactory.decodeByteArray以产生Bitmap,然后可以将其传递到返回.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆