Android TextView加载带有src标签的html字符串

本文探讨如何在Android中使用TextView显示包含src标签的HTML字符串,关键在于运用Html.fromHtml()方法,结合Html.ImageGetter和Html.TagHandler进行图像处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


关键API: Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)



通过ImageGetter来加载 src标签中的图片,废话不多说,直接上代码


1.子线程中 执行 加载图片的逻辑


new Thread(new Runnable() {
                        Message msg = Message.obtain();


                        @Override
                        public void run() {
                            Html.ImageGetter imageGetter = new Html.ImageGetter() {
                                @Override
                                public Drawable getDrawable(String source) {
                                    Drawable drawable = ImageUtil.getImageFromNetwork(source, context);
                                    return drawable;
                                }
                            };
                            CharSequence charSequence = Html.fromHtml(htmlSource, imageGetter, null);
                            msg.what = 0x101;
                            msg.obj = charSequence;
                            mHandler.sendMessage(msg);
                        }
                    }).start();

ImageUtil类中调用方法:
   
    public static String encodeUrl(String url) {
        return Uri.encode(url, "-![.:/,%?&=]");
    }
    /**
     * @param imageUrl
     * @return
     */
    public static Drawable getImageFromNetwork(String imageUrl, Activity context) {


        imageUrl=encodeUrl(imageUrl);


        URL myFileUrl = null;
        Drawable drawable = null;


        try {
            myFileUrl = new URL(imageUrl);




        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);


            conn.connect();
            InputStream is;
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK || conn.getResponseCode() == HttpURLConnection.HTTP_CREATED || conn.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
                is = conn.getInputStream();
            } else {
                is = conn.getErrorStream();
            }
            drawable = Drawable.createFromStream(is, null);




            int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
            int screenHeight = context.getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,


int w= (int) (screenWidth/1.1);
            int h = w / (drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight());


            drawable.setBounds(0, 0, w, h);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
    }


3. Handler中进行处理


Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x101) {
                wv_name.setText((CharSequence) msg.obj);
            }
            return false;
        }
    });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值