问题
I am trying to use LruCache in android to cache some images, but its not caching
here is the code
int cacheSize1 = 4 * 1024 * 1024; // 4MiB
bitmapCache = new LruCache(cacheSize1) {
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}};
here its other methods
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
bitmapCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
Bitmap b = (Bitmap)bitmapCache.get(key);
return b;
}
here I am using them this is my code
for (int i = 0; i < HomeActivity.globalObj.categoriesList.size(); i++) {
ImageView iv = new ImageView(getApplicationContext());
Bitmap bb = getBitmapFromMemCache(HomeActivity.globalObj.categoriesList.get(i).name);
if (bb != null) {
iv.setImageBitmap(bb);
imageViewList.add(iv);
}
else{
Bitmap b = getImageBitmap(HomeActivity.globalObj.categoriesList.get(i).large_image);
addBitmapToMemoryCache(HomeActivity.globalObj.categoriesList.get(i).name, b);
iv.setImageBitmap(b);
imageViewList.add(iv);
}
}
回答1:
Refer this project, it's a Google I/O sample project to explain how to use LRU Cache
.
来源:https://stackoverflow.com/questions/11716214/caching-images-with-lrucache