Android-BitmapCache… the rebirth.

A while ago I wrote Android-BitmapMemoryCache, a cache based off of LruCache which would cache Android Bitmap objects in memory in a clever way. You can get the full details from this earlier post, but the bottom line is that it knew when a Bitmap was no longer being used, and so would recycle it.

I left it like that the past 5 months and I knew it worked. I was aware though, that developers were looking for something a little more complete, so for the past few days I’ve been refactoring Android-BitmapMemoryCache into Android-BitmapCache.


So I introduce Android-BitmapCache, a new multi-level cache for Bitmaps. Currently it has two levels: memory and disk, which are exposed as a single cache for you to use as you wish.

The memory level is pretty much the same as before, nothing much new there. The new disk level is a cache using Jake Wharton’s DiskLruCache, which means you get all of the benefits of that library: solid implementation and bounded disk space usage.


It’s pretty simple to use cache. Just created a BitmapLruCache object and store it somewhere you can access, either the Application object or a static variable:

BitmapLruCache mCache;

// This can be anywhere your app has Read/Write access
File diskCacheLocation = getExternalFilesDir(null);

// Create Builder object
BitmapLruCache.Builder builder = new BitmapLruCache.Builder();

// Enable Memory Cache, and set max size using heap size

// Enable Disk Cache, and set location

// Finally get the build BitmapLruCache
mCache =;

Once your cache has been setup and is accessible, you can start using it! You should use the bundled CacheableImageView wherever possible, as the memory cache has a close relationship with it.

To retrieve a cached value from the given Url, clients can call:


This will check all available caches for the value. There are also the getFromDiskCache(String) and getFromMemoryCache(String) which allow more granular access.

There are a number of update methods:

put(String, InputStream) and <code>put(String, InputStream, boolean)</code>

are the preferred versions of the method, as they allow 1:1 caching to disk of the original content.

put(String, Bitmap) and put(String, Bitmap, boolean)

Should only be used if you can’t get access to the original InputStream.

You can easily have a look at the sample code.


I have previously said that this library would only be a simple caching library, and that I would not add any image fetching to it. Well to make the library easier to use for a novice I’ve going to add this in v2.1. It will not be a hugely complex implementation, but enough to get someone ‘going’.