Disposing raster images in a list causes problems

I got a list view in which im display images, these images are loaded and cached properly. When I look at the list and load new items, I want to recycle the least recently used for saving in memory. I have successfully implemented LRU Map and im call recycle on bitmaps that are not visible on the screen. Here is the piece of 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"); } } 

These values ​​are stored in the getView method of the adapter. The problem is that when I process the images, I see how those that are visible on the screen are also lost (note that only those that I know come out of the screen).

Does anyone have an idea on why I am losing the images currently displayed when I processed the bitmaps of others in the list?

Here I refer to the downloaded bitmap or load 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); } 
+4
source share
1 answer

You must remember that your ImageView inside your convertView (list item) is constantly reused. In short, you are not creating a new convertView every time a new view appears from the ListView , which you actually just reuse the last element that scrolls.

So, in your case, let's say that you have a list in which 3 elements are visible at any time. When you clear item 1, you also clear item 4 and item 7, etc. Etc.

I cannot indicate the exact point where this problem bit you, but based on what you are describing, I am almost sure that this is a problem.

+1
source

All Articles