欢迎大家访问我的博客http://blog.csdn.net/mikejaps,专注于android ios app 开发
A memory cache is useful in speeding up access to recently viewed bitmaps, however you cannot rely on images being available in this cache. Components like GridView
with larger datasets can easily fill up a memory cache. Your application could be interrupted by another task like a phone call, and while in the background it might be killed and the memory cache destroyed. Once the user resumes, your application it has to process each image again.
之前我们说的LruCache用来缓存bitmap速度是非常快的,然而你不可以重复的使用这个cache中的图片,像GriadView组件有时候数据量很大,cache很容易被写满。你应用可能会被其他的任务打断比如电话,这时候你的应用可能在后台被杀死,cache被损毁,当用户在恢复应用的时候,应用必须重新创建这个cache
A disk cache can be used in these cases to persist processed bitmaps and help decrease loading times where images are no longer available in a memory cache. Of course, fetching images from disk is slower than loading from memory and should be done in a background thread, as disk read times can be unpredictable.
在这种情况 disk cache 可以被用来 保存bitmap,减少bitmap的加载次数,但是 内存(Lrucache)中的图片在这种情况下已经是不可用的了。当然,在disk cache中加载图片的速度可能比在内存(Lrucach,ram)中加载要慢,还要在后台线程中执行。但是 disk 读取次数可以没有限制
Note: A ContentProvider
might be a more appropriate place to store cached images if they are accessed more frequently, for example in an image gallery application.
注意:contentprovider 在频繁加载图片中用来保存图片可能更适用,比如 在画廊应用中
Included in the sample code of this class is a basic DiskLruCache
implementation. However, a more robust and recommended DiskLruCache
solution is included in the Android 4.0 source code (libcore/luni/src/main/java/libcore/io/DiskLruCache.java
).
以下是DiskCache的基本应用的示例代码,更多的介绍可以查看Android 4.0源码(libcore/luni/src/main/java/libcore/io/DiskLruCache.java
).
Here’s updated example code that uses the simple DiskLruCache
included in the sample application of this class:
以下是示例代码,
private DiskLruCache mDiskCache; private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB private static final String DISK_CACHE_SUBDIR = "thumbnails"; @Override protected void onCreate(Bundle savedInstanceState) { ... // Initialize memory cache ... File cacheDir = getCacheDir(this, DISK_CACHE_SUBDIR); mDiskCache = DiskLruCache.openCache(this, cacheDir, DISK_CACHE_SIZE); ... } class BitmapWorkerTask extends AsyncTask { ... // Decode image in background. @Override protected Bitmap doInBackground(Integer... params) { final String imageKey = String.valueOf(params[0]); // Check disk cache in background thread Bitmap bitmap = getBitmapFromDiskCache(imageKey); if (bitmap == null) { // Not found in disk cache // Process as normal final Bitmap bitmap = decodeSampledBitmapFromResource( getResources(), params[0], 100, 100)); } // Add final bitmap to caches addBitmapToCache(String.valueOf(imageKey, bitmap); return bitmap; } ... } public void addBitmapToCache(String key, Bitmap bitmap) { // Add to memory cache as before if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } // Also add to disk cache if (!mDiskCache.containsKey(key)) { mDiskCache.put(key, bitmap); } } public Bitmap getBitmapFromDiskCache(String key) { return mDiskCache.get(key); } // Creates a unique subdirectory of the designated app cache directory. Tries to use external // but if not mounted, falls back on internal storage. public static File getCacheDir(Context context, String uniqueName) { // Check if media is mounted or storage is built-in, if so, try and use external cache dir // otherwise use internal cache dir final String cachePath = Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED || !Environment.isExternalStorageRemovable() ? context.getExternalCacheDir().getPath() : context.getCacheDir().getPath(); return new File(cachePath + File.separator + uniqueName); }
While the memory cache is checked in the UI thread, the disk cache is checked in the background thread. Disk operations should never take place on the UI thread. When image processing is complete, the final bitmap is added to both the memory and disk cache for future use.
ram cache 是在UI线程中使用,disk cache 在后台线程中使用,disk 的操作永远不能在UI线程中操作,当图片处理完,最后的bitmap 就被加载到ram中,和被以后使用的disk中
DiskLruCache 下载地址 点击打开链接 http://download.csdn.net/detail/mikejaps/9228615