Mark Tyers Mark Tyers - 11 months ago 48
Android Question

Efficiently download and use images in recyclerview

Currently I'm using this to download and display the images in my recyclerview:

private class RetrieveImageTask extends AsyncTask<URL,Void,Drawable>{
URL url;
InputStream inputStream;
protected Drawable doInBackground(URL... urls){
url = urls[0];
try {
inputStream = (InputStream)url.getContent();
} catch (IOException e) {
return Drawable.createFromStream(inputStream, null);
protected void onPostExecute(Drawable drawable) {


I then use this class in
in my RecyclerViewAdapter:

public void onBindViewHolder(ItemViewHolder item, int i) {
try {
URL myUrl = new URL(aushangdata.getJSONObject(i).getString("image"));
RetrieveImageTask task = new RetrieveImageTask();
Drawable drawable = task.execute(myUrl).get();
} catch (JSONException | IOException | InterruptedException | ExecutionException e) {

This is obviously an extremely shitty solution, and it is very noticeable: when I open the fragment containing the recyclerview, I get a lag of up to 0.5 seconds, and when I scroll down fast it's also lagging because the images take up a lot of space on the cards.

My quesiton is: What is the most optimal way to download and display images in a

Answer Source

You can use Glide:

Also is so simple to use:

Glide.with(CONTEXT).load(URL or DRAWABLE).asBitmap().into(IMAGEVIEW);

Check the wiki for more options:

In your code:


Hope it helps.