2

i have the following code to cache bitmaps , when i try to retrieve them using their key . They always return null . Please help me . Thank You

 final int memClass = ((ActivityManager) mcontext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    final int cacheSize =  1024 *  1024 *memClass ;
    Toast.makeText(mcontext,"Max Cache "+cacheSize,Toast.LENGTH_LONG).show();
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };

Functions to get and set cache

 public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        Toast.makeText(mcontext,"CEO "+key+" - "+bitmap,Toast.LENGTH_LONG).show();
        mMemoryCache.put(key, bitmap);
    }
    else
    {
        Toast.makeText(mcontext,"NULL",Toast.LENGTH_LONG).show();
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

-----------UPDATE----------------------------------------- Well after some debugging found out that it is inserting the value , but i cannot access it after i have exited the app. When i try to relaunch the app the cache is lost. Please could someone help me how to retain that value . Thank You

Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27

1 Answers1

1

I may be wrong, but from my understunding the cache is made to mantain some data in your RAM to remove charge from the CPU. Cache is acumulated during application lifetime (load same photos multiple times, etc). It is normal behaviour to clear the cache after the application is shot down.

Lukasz Kruszyna
  • 1,734
  • 1
  • 12
  • 6