I got a list view where im displaying images, these images are downloaded and cache properly. As I scroll down the list and load new items I want to recycle the least recently used ones to save on memory. I have successfully implemented the LRU Map and im calling recycle on bitmaps which are not visible on the screen. Here is part of the code:
imageView is a ViewGroup
public void recycleImage(int res) {
if (imageView != null) {
imageView.setBackgroundDrawable(null);
if (res > 0)
imageView.setBackgroundResource(res);
}
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
Log.i(TAG, "Bitmap recycled");
}
}
Those values are saved on the getView method of the adapter. The problem is that as I recycle images I can see how the ones visible on the screen also get lost (note im only recycling the ones I know are out of the screen).
Does anyone have an idea of why im losing the images currently displayed when I recycled the bitmaps of others on the listview?
This is where I access the stored bitmap or download it.
if (mImageMap.containsKey(url)) {
ImageCacheModel cache = mImageMap.get(url);
return cache.getBitmap();
} else if (cacheManager.isUrlCached(url)) {
mImageMap.put(url, new ImageCacheModel(imageView, cacheManager.getCachedBitmapOrNull(url, 2)));
ImageCacheModel cache = mImageMap.get(url);
return cache.getBitmap();
} else {
cacheManager.addUrlToQueue(url, this, true);
}
You must remember that your ImageView
inside your convertView
(list element) is being reused all the time. In short you don't create a new convertView
every time a new view shows up from the ListView
you actually just reuse the last element that scrolled out.
So in your case lets say you have a list where 3 items are visible at any time. When you clear item 1 you also clear item 4 and item 7 etc. etc.
I can't point to the exact point where this issue bites you but based on what you describe I'm almost certain this is the issue.