ListAdapter and background Threading

354 views Asked by At

I have a listadapter, where I have many listitems, every item contains a textview and a imageview. I would like to download the images (encoded in Base64) from the backend, so Picasso and the via URL downloading solutions are not good for me. After I downloaded the Base64 decode and Bitmap creation is already done, I`m just stuck how to start a background thread in a list adapter, where views are always recycling (when user scrolls).

I started to created a Thread with a Hanlder, where handler inserted the bitmap to the imageview, but it didn`t worked (out of memoryE). After it I fired an AsyncTask, but in this case I have the recycle problem, namley: when the user scroll down, I see the top image in a bottom view :(

using retrofit for downloading

Can you help me please ?

Code of my AsyncTaks: The problem here, I update the imageview, but maybe the user are already scrolled away!

public DownloadImageAsyncTask(ImageView iw, Display display, String imageID, ImagesCache imagesCache) {
    this.iw = iw;
    this.imageCache = imagesCache;
    this.imageID = imageID;
    this.display = display;
}
@Override
protected Void doInBackground(Void... params) {
    String[] imageArray = new String[1];
    imageArray[0] = imageID;
    ImageResponse imageResponse = new IdeaBackend().getImageByID(imageArray);
    bitmap = UserExperienceHelper.decodeBase64AndScaleDownImage(imageResponse.getResponse().get(0).getImageBase64Code(), display);
    imageCache.put(imageID, bitmap);

    return null;
}


@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    this.iw.setImageBitmap(bitmap);
}
3

There are 3 answers

0
Ramandeep Nanda On

You need to recycle bitmaps or views. Incremental loading of listviews can easily be handled. Here's a post that might help you!

Optimized, Incremental Access to ListView.

Endless Scrolling with adapter views

0
narancs On

First of all I would like to thank you your comments and ideas about solution. I was working on this in the last two days, when I asked you, so actually I coming up with my final solution, what was partly ready when I posted here:

public class IdeaAdapter extends ArrayAdapter<Idea> {

private final Context context;
private final Display display;
private List<Idea> dataList;
private Set<String> imagesInProgress;


public IdeaAdapter(Display display, Activity c, List<Idea> dataList) {
    super(c, android.R.layout.simple_list_item_1, dataList);
    this.dataList = dataList;
    this.context = c;
    this.display = display;
    imagesInProgress = new HashSet<>();
}


@Override
public int getCount() {
    return dataList.size();
}

@Override
public Idea getItem(int position) {
    return dataList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
        vh = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.idea_list_item, null);
        vh.ideaImage = (ImageView) convertView.findViewById(R.id.idea_image);
        vh.ideaName = (TextView) convertView.findViewById(R.id.idea_title);
        vh.progressWheel = (ProgressWheel) convertView.findViewById(R.id.progress_wheel);


        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

    final Idea ideaItem = dataList.get(position);
    vh.ideaName.setText(ideaItem.getTitle());


    if (ideaItem.getImages().size() > 0) {
        final String imageId = ideaItem.getImages().get(0);
        Log.e("  isContains", String.valueOf(ImagesCache.getInstance().containsKey(String.valueOf(imageId))));

        if (!ImagesCache.getInstance().containsKey(String.valueOf(imageId)) && !imagesInProgress.contains(imageId)) {

            Log.e("  downloading", String.valueOf(imageId));

            vh.ideaImage.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
            imagesInProgress.add(imageId);
            new DownloadImageAsyncTask(vh.ideaImage, display, imageId).execute();
        } else {
            Log.e("CACHE", String.valueOf(imageId));
            vh.ideaImage.setImageBitmap(ImagesCache.getInstance().getBitmap(String.valueOf(imageId)));
        }
    }


    return convertView;
}

private static class ViewHolder {
    private ImageView ideaImage;
    private TextView ideaName;
    private ProgressWheel progressWheel;

}

}

Image Cache:

public class ImagesCache {
private static ImagesCache INSTANCE;
private static Context context;
private static DiskLruCache mDiskCache;
private static Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
private static int mCompressQuality = 70;
private static final int APP_VERSION = 1;
private static final int VALUE_COUNT = 1;
private static final String TAG = "DiskLruImageCache";
private static final String uniqueName = "IdeasCache";
private static final int diskCacheSize = 20*1024*1024; // in bytes

/**
 * Make sure to give an application context to me.
 * I like to keep it as a static reference
 *
 * @param c
 */
public static void init(Context c) {
    context = c;
}

public static ImagesCache getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new ImagesCache();
    }
    return INSTANCE;
}

private ImagesCache() {
    try {
        final File diskCacheDir = getDiskCacheDir(context, uniqueName);
        mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize);
        /*mCompressFormat = compressFormat;
        mCompressQuality = quality;*/
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
        throws IOException {
    OutputStream out = null;
    try {
        out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE);
        return bitmap.compress(mCompressFormat, mCompressQuality, out);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

private File getDiskCacheDir(Context context, String uniqueName) {

    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !Utils.isExternalStorageRemovable() ?
                    Utils.getExternalCacheDir(context).getPath() :
                    context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}

public static void put(String key, Bitmap data) {

    DiskLruCache.Editor editor = null;
    try {
        editor = mDiskCache.edit(key);
        if (editor == null) {
            return;
        }

        if (writeBitmapToFile(data, editor)) {
            mDiskCache.flush();
            editor.commit();
            if (BuildConfig.DEBUG) {
                Log.d("cache_test_DISK_", "image put on disk cache " + key);
            }
        } else {
            editor.abort();
            if (BuildConfig.DEBUG) {
                Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key);
            }
        }
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key);
        }
        try {
            if (editor != null) {
                editor.abort();
            }
        } catch (IOException ignored) {
        }
    }

}

public static Bitmap getBitmap(String key) {

    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = null;
    try {

        snapshot = mDiskCache.get(key);
        if (snapshot == null) {
            return null;
        }
        final InputStream in = snapshot.getInputStream(0);
        if (in != null) {
            final BufferedInputStream buffIn =
                    new BufferedInputStream(in, Utils.IO_BUFFER_SIZE);
            bitmap = BitmapFactory.decodeStream(buffIn);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (snapshot != null) {
            snapshot.close();
        }
    }

    if (BuildConfig.DEBUG) {
        Log.d("cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
    }

    return bitmap;

}

public static boolean containsKey(String key) {

    boolean contained = false;
    DiskLruCache.Snapshot snapshot = null;
    try {
        snapshot = mDiskCache.get(key);
        contained = snapshot != null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (snapshot != null) {
            snapshot.close();
        }
    }

    return contained;

}

public static void clearCache() {
    if (BuildConfig.DEBUG) {
        Log.d("cache_test_DISK_", "disk cache CLEARED");
    }
    try {
        mDiskCache.delete();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static File getCacheFolder() {
    return mDiskCache.getDirectory();
}

}

All I need to do, managing threads (blocking queue) to not fire for each of getView calls !

1
Shivam Verma On

In your adapter :

View listItemLayout = (View) inflater.inflate(...);
ImageView imageView = (ImageView) listItemLayout.findViewById(R.id.imageView);
MyImageLoader.loadImage(activity, imageView, id);

This is public class that will do the downloading and caching of image for you.

public class MyImageLoader {

    private HashMap < String, Bitmap > cache = new HashMap < String, Bitmap > ();

    public static loadImage(final Activity activity, final ImageView imageView, final String id) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (cache.containsKey(id) {
                    //Bitmap already exists. 
                } else {
                    //This bitmap has not been downloaded so you need to download the bitmap.
                    //Once bitmap is downloaded, add it to the HashMap.

                }

                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        imageView.setImageBitmap(cache.get(id));
                    }
                });
            }
        }).start();
    }
}

Now, whenever you do imageView.setImageBitmap() the image will automatically get updated. You don't need to refresh the view separately.

A few more additions to the previous code :

public class MyImageLoader {

    private static HashMap <String, Bitmap> cache = new HashMap <String, Bitmap>();
    private static HashMap<String, DownloadListener> listeners = new HashMap()<>;
    private static ArrayList<String> currentlyDownloading = new ArrayList()<>;

    public static loadImage(final Activity activity, final ImageView imageView, final String id) {

        DownloadListener downloadListener = new DownloadListener(){
            @Override
            public void onDownloadComplete(){
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        imageView.setImageBitmap(cache.get(id));
                    }
                }); 
            }

            @Override
            public void onDownloadFailed(){
                // Do something when download fails. 
            }
        };

        if(!listeners.containsKey(id)){
            listener.put(id, new ArrayList<DownloadListener>());
        } 

        listener.get(id).add(downloadListener);

        if(cache.containsKey(id)){
            for(DownloadListener listener : listeners.get(id)){
                listener.onDownloadComplete();
            }                
        } else if(!currentlyDownloading.contains(id)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    currentlyDownloading.add(id);
                    //Start Downloading 
                    //Download finished
                    currentlyDownloading.remove(id);
                    //If download successful
                    cache.put(id, bitmap);
                    for(DownloadListener listener : listeners.get(id)){
                        listener.onDownloadComplete();
                    }  
                    /*
                    If download failed
                    for(DownloadListener listener : listeners.get(id)){
                        listener.onDownloadFailed();
                    }  
                    */
                    currentlyDownloading.remove(id);
                }
            }).start();
        }
    }

    public interface DownloadListener {

        public void onDownloadComplete();

        public void onDownloadFailed();
    }
}