shiami shiami - 10 months ago 66
Android Question

Display images on Android using TextView and Html.ImageGetter asynchronously?

I want to set a

TextView
with
SpannableString
which is from the method below:

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


But the
ImageGetter
here need to override the method below:

public abstract Drawable getDrawable(String source)


Because I need to get the drawable from the internet, I have to do it asynchronously and seems it is not.

How to make it work?
Thanks.

Answer Source

Now I'm using an AsyncTask to download the images in the ImageGetter:

Spanned spannedContent = Html.fromHtml(htmlString, new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
            new ImageDownloadAsyncTask().execute(textView, htmlString, source);
            return null;
        }
    }, null);

And set the text again into the TextView when the image has been downloaded.

Now it works. But It failed when I tried to do the TextView.postInvalidate() to redraw the downloaded images. I have to do setText() again in the AsyncTask.

Does anyone know why?