'How to make an Image Getter in a RecyclerView using fromHTML more efficient?
I'm using fromHTML to load HTML text into a TextView inside a RecyclerView element. This does work, however it is incredibly slow as the images are being loaded from a URL and when the adapter is set or updated the app freezes up or crashes and I get logs saying too much work is being done on the main thread. Is there any way to make the ImageGetter in HTML.fromHTML that I'm using more efficient and make the adapter run on a seperate thread when updating so it doesn't freeze up the app? I've tried looking at faster ways to get a Drawable but the main image loading libraries don't seem to have a way to return a Drawable object.
Solution 1:[1]
It looks like the line Boolean result = fiu.execute().get();
is blocking the UI thread and is defeating the purpose of the AsyncTask. You should consider using a callback in onPostExecute()
to set the image.
Unfortunately, there is not a lot of information online about how to implement Html.ImageGetter. I answered a question here that addresses changing the placeholder image while the actual image loads on a simulated background thread. The solution presented in the question also works but requires manipulating and rescanning the HTML text.
I think that you could adapt something from that post to solve your problem.
Update: I think a better solution would be to use a library such as Glide to load the drawable. If the library doesn't handle your use case natively, take a look at how others have handled the limitation. See here for one approach. I think that this Stack Overflow answer may be useful to you. In onResourceReady()
you can set the text with the loaded image.
Second update: I would look into Glide more. You can create a custom loader called a "ModelLoader" that can fetch your stream drawables. See here regarding how to write a ModelLoader and here for an example. (Don't know if that example works or not, but it looks OK.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |