Back to project page ImageGrabber.
The source code is released under:
Apache License
If you think the Android project ImageGrabber listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.herroworld.imagegrabber; /* w ww . j a va 2s. co m*/ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.support.v4.util.LruCache; import android.util.Log; import java.io.File; /** * This class holds our bitmap caches (memory and disk). */ public class ImageCache { private static final String TAG = "ImageCache"; private DiskLruCache mDiskCache; private LruCache<String, Bitmap> mMemoryCache; /** * Creating a new ImageCache object using the specified parameters. * * @param context The context to use * @param cacheParams The cache parameters to use to initialize the cache */ public ImageCache(Context context, ImageCacheParams cacheParams) { init(context, cacheParams); } /** * Creating a new ImageCache object using the default parameters. * * @param context The context to use * @param uniqueName A unique name that will be appended to the cache * directory */ public ImageCache(Context context, String uniqueName) { init(context, new ImageCacheParams(uniqueName)); } /** * Initialize the cache, providing all parameters. * * @param context The context to use * @param cacheParams The cache parameters to initialize the cache */ private void init(Context context, ImageCacheParams cacheParams) { final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.uniqueName); // Set up disk cache if (cacheParams.diskCacheEnabled) { mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize); mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality); if (cacheParams.clearDiskCacheOnStart) { mDiskCache.clearCache(); } } // Set up memory cache if (cacheParams.memoryCacheEnabled) { mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) { /** * Measure item size in bytes rather than units which is more * practical for a bitmap cache */ @Override protected int sizeOf(String key, Bitmap bitmap) { return Utils.getBitmapSize(bitmap); } }; } } public void addBitmapToCache(String data, Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add to memory cache if (mMemoryCache != null && mMemoryCache.get(data) == null) { mMemoryCache.put(data, bitmap); } // Add to disk cache if (mDiskCache != null && !mDiskCache.containsKey(data)) { mDiskCache.put(data, bitmap); } } /** * Get from memory cache. * * @param data Unique identifier for which item to get * @return The bitmap if found in cache, null otherwise */ public Bitmap getBitmapFromMemCache(String data) { if (mMemoryCache != null) { final Bitmap memBitmap = mMemoryCache.get(data); if (memBitmap != null) { if (Utils.DEBUG) { Log.d(TAG, "Memory cache hit"); } return memBitmap; } } return null; } /** * Get from disk cache. * * @param data Unique identifier for which item to get * @return The bitmap if found in cache, null otherwise */ public Bitmap getBitmapFromDiskCache(String data) { if (mDiskCache != null) { return mDiskCache.get(data); } return null; } public void clearCaches() { mDiskCache.clearCache(); mMemoryCache.evictAll(); } /** * A holder class that contains cache parameters. */ public static class ImageCacheParams { public String uniqueName; public int memCacheSize = 1024 * 1024 * 5; // 5MB public int diskCacheSize = 1024 * 1024 * 10; // 10MB public CompressFormat compressFormat = CompressFormat.JPEG; public int compressQuality = 70; public boolean memoryCacheEnabled = true; public boolean diskCacheEnabled = true; public boolean clearDiskCacheOnStart = false; public ImageCacheParams(String uniqueName) { this.uniqueName = uniqueName; } } }