关于Volley图片的三级缓存的基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31848763/article/details/51693946

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

猜你喜欢

转载自blog.csdn.net/qq_31848763/article/details/51693946