Rich text displaying pictures in TextView

Recently, rich text has been used as a requirement in the project. In android, we set TextView to display rich text, which is relatively simple if it doesn't involve pictures

TextView tv = new TextView(this);
Spanned spanned = Html.fromHtml(content);
tv.setText(spanned);

However, if there are pictures in html, the following situations will occur, and spaces will appear in the places where the pictures originally appeared:

So you need to process the image when filling in rich text. Html provides two methods

Html.fromHtml(String source);
Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler);

Among them, the three parameter method fromHtml can deal with the problem of pictures. It needs an anonymous inner class to implement ImageGetter, and the third parameter can be set to null;

At the same time, because the internal class of ImageGetter obtains the network picture and fills in rich text according to the URL, it is a time-consuming operation, which needs to be implemented in the sub thread, and the setTest of TextView is the operation to update the UI, so the Handler can be used, and the complete code is as follows:

private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    CharSequence charSequence = (CharSequence) msg.obj;
                    if (charSequence != null) {
                        hotActivityContent.setText(charSequence);
                        hotActivityContent.setMovementMethod(LinkMovementMethod.getInstance());
                    }
                    break;
                default:
                    break;
            }
        }
};
private void setActivityContent(final String activityContent) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Html.ImageGetter imageGetter = new Html.ImageGetter() {

                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable;
                        drawable = ImageUtils.getInstance().getImageNetwork(source);
                        if (drawable != null) {
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        } else if (drawable == null) {
                            return null;
                        }
                        return drawable;
                    }
                };
                CharSequence charSequence = Html.fromHtml(activityContent.trim(), imageGetter, null);
                Message ms = Message.obtain();
                ms.what = 1;
                ms.obj = charSequence;
                mHandler.sendMessage(ms);
            }
        }).start();
}
/**
     * Connect to the network to get the corresponding pictures
     * @param imageUrl
     * @return
     */
public Drawable getImageNetwork(String imageUrl) {
        URL myFileUrl = null;
        Drawable drawable = null;
        try {
            myFileUrl = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            // In this step, it is better to compress the pictures first to avoid excessive memory consumption
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            drawable = new BitmapDrawable(bitmap);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
}

Tags: network Android

Posted on Fri, 20 Mar 2020 08:56:12 -0700 by Sturm