关于Volley图片的三级缓存的基本使用
2016-06-16 18:37
483 查看
概述
前不久使用volley时用到的图片的三级缓存,分享下。
其中用到的DisLruCache将图片的URL进行MD5编码,
参考郭霖的:http://blog.csdn.net/guolin_blog/article/details/28863651
源码下载地址:
http://download.csdn.net/detail/qq_31848763/9551770
前不久使用volley时用到的图片的三级缓存,分享下。
其中用到的DisLruCache将图片的URL进行MD5编码,
参考郭霖的:http://blog.csdn.net/guolin_blog/article/details/28863651
package com.example.volleycache; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; import android.util.LruCache; import com.android.volley.toolbox.ImageLoader.ImageCache; /* * Volley中图片的本地缓存和内存缓存的使用; */ public class LruImageCache implements ImageCache { //内存缓存 LruCache<String, Bitmap> lrucache; //本地缓存 DiskLruCache disklrucache; //本地缓存文件名 String DISK_CACHE_DIR = "volley_image"; //本地缓存大小 final long DISK_MAX_SIZE = 20 * 1024 * 1024; private static LruImageCache lruImageCache = null; public static LruImageCache getLruImageCache(Context context) { if (lruImageCache == null) { synchronized (Object.class) { if (lruImageCache == null) { lruImageCache = new LruImageCache(context); } } } return lruImageCache; } private LruImageCache(Context context) { // TODO Auto-generated constructor stub // 获取应用内存; final int maxMemory = (int) Runtime.getRuntime().maxMemory(); final int cacheSize = maxMemory / 8; // 初始化LruCache,设置缓存大小cacheSize; this.lrucache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { // TODO Auto-generated method stub return value.getRowBytes() * value.getHeight(); } }; /* * 本地缓存 */ final String disLruCachePath = context.getCacheDir().getPath(); //缓存路径 File disLruCachedir = new File(disLruCachePath + File.separator + DISK_CACHE_DIR); if (!disLruCachedir.exists()) { disLruCachedir.mkdirs(); } try { // 初始化DiskLruCache,设置最缓存大小DISK_MAX_SIZE; /*参数一:路径 * 参数二:应用程序版本号 * 参数三:缓存文件数量 * 参数四:缓存大小 */ disklrucache = DiskLruCache.open(disLruCachedir, 1, 1, DISK_MAX_SIZE); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public Bitmap getBitmap(String url) { // TODO Auto-generated method stub String key = MD5Utils.hashKeyForDisk(url); // 先从内存中找 Bitmap bitmap = lrucache.get(url); if (bitmap == null) { //本地 bitmap = getBitmap_DiskLruCache(key); // 放入内存 if (bitmap != null) { lrucache.put(url, bitmap); } } return bitmap; } @Override public void putBitmap(String url, Bitmap bitmap) { // TODO Auto-generated method stub lrucache.put(url, bitmap); String key = MD5Utils.hashKeyForDisk(url); putBitmap_DiskLruCache(key, bitmap); } // 写入本地 private void putBitmap_DiskLruCache(String key, Bitmap bitmap) { // TODO Auto-generated method stub DiskLruCache.Editor editor = null; OutputStream outputStream = null; try { editor = disklrucache.edit(key); if (editor != null) { outputStream = editor.newOutputStream(0); boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); if (compress) { disklrucache.flush(); editor.commit(); } else { editor.abort(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (editor != null) { editor.abort(); } if (outputStream != null) { outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } } // 从本地读取 private Bitmap getBitmap_DiskLruCache(String key) { // TODO Auto-generated method stub DiskLruCache.Snapshot snapshot = null; Bitmap bitmap = null; InputStream inputStream = null; try { snapshot = disklrucache.get(key); if (snapshot != null) { inputStream = snapshot.getInputStream(0); if (inputStream != null) { bitmap = BitmapFactory.decodeStream(inputStream); } } } catch (IOException e) { e.printStackTrace(); } finally { if (snapshot != null) { snapshot.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return bitmap; } }
源码下载地址:
http://download.csdn.net/detail/qq_31848763/9551770
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories