在Html.fromHtml加载图像中的TextView(HTTP URL图像,Base64的网址图像) [英] Load images in Html.fromHtml in textview (http url images, Base64 url images)

查看:154
本文介绍了在Html.fromHtml加载图像中的TextView(HTTP URL图像,Base64的网址图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView中,我展示了一个论坛帖子,这是使用RTE进入网站上的内容,内容涉及形象与的Base64类型的两种类型的Web网址, .. Html.fromHtml的默认实现替换所有 IMG 标签与小方..

I have a textview in which I am showing content of a forum post, which is entered on website using rte, the content involves images, both of type web url and of type Base64.. The default implementation of Html.fromHtml replaces all the img tags with small square..

我看着就那么对于解决​​方案来加载使用Html.fromHtml方法从URL的图像,原来有办法做到这一点,我们可以通过一个ImageGetter给函数。我发现的this~~V 真棒答案,实现了网址提取部分,但是这种失败和崩溃时,内容有的图像的Base64 <应用程序/ code> ..

I looked on SO for solution to load images from url using Html.fromHtml method, turns out there is way to do it, we can pass a ImageGetter to the function.. I found this awesome answer which implements the url fetch part, but this fails and crashes the app when the content has an image of Base64..

我看了一个方法来创建图像的Base64 src,却没有任何解决方案都工作,如果有人实施了整体解决方案,其巨大的。如果有人在的Base64 部分,请提供,我将整合双方..

I looked a way to create images for Base64 src but none of the solutions are working, If anyone has implemented the whole solution its great. If someone has just the Base64 part please provide that I'll integrate both..

推荐答案

在这花几个小时我已经找到了解决方案的的Base64 图像后,终于......我张贴这里完整的解决方案。

Finally after spending hours on this I have found the solution to the Base64 image.. I am posting the complete solution here..

我想再次感谢 http://stackoverflow.com/a/15617341/1114536 为基回答..

I would once again like to thank http://stackoverflow.com/a/15617341/1114536 for the base answer..

原来我是用作为参考这asnwer ..

Turns out the answer I was using as reference was just copy of this asnwer..

URLDrawable.java

import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class URLDrawable extends BitmapDrawable {
    // the drawable that you need to set, you could set the initial drawing
    // with the loading image if you need to
    protected Drawable drawable;

    @Override
    public void draw(Canvas canvas) {
        // override the draw to facilitate refresh function later
        if(drawable != null) {
            drawable.draw(canvas);
        }
    }
}

URLImageParser.java

import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.util.Base64;
import android.view.View;

public class URLImageParser implements ImageGetter {
    Context context;
    View container;

    public URLImageParser(View container, Context context) {
        this.context = context;
        this.container = container;
    }

    public Drawable getDrawable(String source) {   
        if(source.matches("data:image.*base64.*")) {
            String base_64_source = source.replaceAll("data:image.*base64", "");
            byte[] data = Base64.decode(base_64_source, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);                
            Drawable image = new BitmapDrawable(context.getResources(), bitmap);
            image.setBounds(0, 0, 0 + image.getIntrinsicWidth(), 0 + image.getIntrinsicHeight()); 
            return image;
        } else {
            URLDrawable urlDrawable = new URLDrawable();
            ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
            asyncTask.execute(source);          
            return urlDrawable; //return reference to URLDrawable where We will change with actual image from the src tag
        }        
    }

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
        URLDrawable urlDrawable;

        public ImageGetterAsyncTask(URLDrawable d) {
            this.urlDrawable = d;
        }

        @Override
        protected Drawable doInBackground(String... params) {
            String source = params[0];
            return fetchDrawable(source);
        }

        @Override
        protected void onPostExecute(Drawable result) {         
            urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight()); //set the correct bound according to the result from HTTP call            
            urlDrawable.drawable = result; //change the reference of the current drawable to the result from the HTTP call          
            URLImageParser.this.container.invalidate(); //redraw the image by invalidating the container
        }

        public Drawable fetchDrawable(String urlString) {
            try {
                InputStream is = (InputStream) new URL(urlString).getContent();
                Drawable drawable = Drawable.createFromStream(is, "src");
                drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight()); 
                return drawable;
            } catch (Exception e) {
                return null;
            } 
        }
    }
}

用法:

TextView comment_content_container = ((TextView)findViewById(R.id.comment_content));
comment_content_container.setText(Html.fromHtml(comment.content, new URLImageParser(comment_content_container, this), null));

如果有人知道了的Base64 ,请回复我会更新的答案更好的正则表达式。

If anyone knows better regex for the Base64, please reply I will update the answer..

这篇关于在Html.fromHtml加载图像中的TextView(HTTP URL图像,Base64的网址图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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