我最近在写一个手机上浏览网页的客户端程序,碰到了一个让人很头疼的问题,就是显示网页中的图片信息。通过查找资料,经过一下午的努力终于实现了,在此总结一下:

1、解析html文件,找到想要显示的模块。
2、用Html类的fromHtml(String source,Html.ImageGetter p_w_picpathGetter,Html.TagHandler tagHandler)方法解析此html模块。

参数:source-要解析的html代码块

        p_w_picpathGetter:处理html代码快中的img标签的类。需要在程序代码中声明一个该类的对象。

        tagHandler:是用来处理那些html解析器未知的标签。

3、调用textview的setText(String text)方法,将以上处理结果设置为此textView对象的文本内容。

说明:p_w_picpathGetter的声明方式如下:

p_w_picpathGetter = new Html.ImageGetter() {

            public Drawable getDrawable(String source) {
                Drawable drawable = null;
                URL url;
                try {
                    url = new URL(source);
                    drawable = Drawable
                            .createFromStream(url.openStream(), "");
                } catch (Exception e) {
                      return null;
                }
                int width = drawable.getIntrinsicWidth();
                int height = drawable.getIntrinsicHeight();
                drawable.setBounds(0, 0, width, height);
                return drawable;
            }
        };