1. 程式人生 > >關於Volley圖片的三級快取的基本使用

關於Volley圖片的三級快取的基本使用

概述
前不久使用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