caching images with LruCache

Refresh

December 2018

Views

2k time

2

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 answers

1

Смотрите этот проект , это / O пример проекта Google я объясню , как использовать LRU Cache.