com.herroworld.imagegrabber
Class DiskLruCache

java.lang.Object
  extended by com.herroworld.imagegrabber.DiskLruCache

public class DiskLruCache
extends java.lang.Object

A simple disk LRU bitmap cache to illustrate how a disk cache would be used for bitmap caching. A much more robust and efficient disk LRU cache solution can be found in the ICS source code (libcore/luni/src/main/java/libcore/io/DiskLruCache.java) and is preferable to this simple implementation.


Method Summary
 void clearCache()
          Removes all disk cache entries from this instance cache dir
static void clearCache(Context context, java.lang.String uniqueName)
          Removes all disk cache entries from the application cache directory in the uniqueName sub-directory.
 boolean containsKey(java.lang.String key)
          Checks if a specific key exist in the cache.
static java.lang.String createFilePath(java.io.File cacheDir, java.lang.String key)
          Creates a constant cache file path given a target cache directory and an image key.
 java.lang.String createFilePath(java.lang.String key)
          Create a constant cache file path using the current cache directory and an image key.
 Bitmap get(java.lang.String key)
          Get an image from the disk cache.
static java.io.File getDiskCacheDir(Context context, java.lang.String uniqueName)
          Get a usable cache directory (external if available, internal otherwise).
static DiskLruCache openCache(Context context, java.io.File cacheDir, long maxByteSize)
          Used to fetch an instance of DiskLruCache.
 void put(java.lang.String key, Bitmap data)
          Add a bitmap to the disk cache.
 void setCompressParams(CompressFormat compressFormat, int quality)
          Sets the target compression format and quality for images written to the disk cache.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

openCache

public static DiskLruCache openCache(Context context,
                                     java.io.File cacheDir,
                                     long maxByteSize)
Used to fetch an instance of DiskLruCache.

Parameters:
context -
cacheDir -
maxByteSize -
Returns:

put

public void put(java.lang.String key,
                Bitmap data)
Add a bitmap to the disk cache.

Parameters:
key - A unique identifier for the bitmap.
data - The bitmap to store.

get

public Bitmap get(java.lang.String key)
Get an image from the disk cache.

Parameters:
key - The unique identifier for the bitmap
Returns:
The bitmap or null if not found

containsKey

public boolean containsKey(java.lang.String key)
Checks if a specific key exist in the cache.

Parameters:
key - The unique identifier for the bitmap
Returns:
true if found, false otherwise

clearCache

public void clearCache()
Removes all disk cache entries from this instance cache dir


clearCache

public static void clearCache(Context context,
                              java.lang.String uniqueName)
Removes all disk cache entries from the application cache directory in the uniqueName sub-directory.

Parameters:
context - The context to use
uniqueName - A unique cache directory name to append to the app cache directory

getDiskCacheDir

public static java.io.File getDiskCacheDir(Context context,
                                           java.lang.String uniqueName)
Get a usable cache directory (external if available, internal otherwise).

Parameters:
context - The context to use
uniqueName - A unique directory name to append to the cache dir
Returns:
The cache dir

createFilePath

public static java.lang.String createFilePath(java.io.File cacheDir,
                                              java.lang.String key)
Creates a constant cache file path given a target cache directory and an image key.

Parameters:
cacheDir -
key -
Returns:

createFilePath

public java.lang.String createFilePath(java.lang.String key)
Create a constant cache file path using the current cache directory and an image key.

Parameters:
key -
Returns:

setCompressParams

public void setCompressParams(CompressFormat compressFormat,
                              int quality)
Sets the target compression format and quality for images written to the disk cache.

Parameters:
compressFormat -
quality -