图片三级缓存仅适配器

public class MyLruCache {
    private String TAG = "22";

    // 声明强引用LruCache引用 范型为String此键用来存储图片对应网址作为键,bitmap对象为值
    private LruCache<String, Bitmap> lrucache;
    // 创建一个HashMap对象 同理范型为String此键用来存储图片对应网址作为键,值为软引用并存储bitmap对象
    private HashMap<String, SoftReference<Bitmap>> map = new HashMap();
    // 声明一个线程池用来同时下载多张图片
    private Executor executor;
    // 首先定义好要存储图片的SD卡的路径方便引用
    private String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/zzh";
    // 定义出强引用的最大大小为当前运行时内存的八分之一
    int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);

    private MyLruCache() {
        // 创建强引用lrucache对象并指明大小和重写方法
        lrucache = new LruCache<String, Bitmap>(maxSize) {

            /**
             * 当LruCache删除图片时就会运行此方法
             *
             * @param evicted
             *            是否为系统自动删除
             * @param key
             *            被删除的键
             * @param oldValue
             *            被删除的值
             * @param newValue
             *            新加入数据
             * @return
             */
            @Override
            protected void entryRemoved(boolean evicted, String key,
                    Bitmap oldValue, Bitmap newValue) {
                super.entryRemoved(evicted, key, oldValue, newValue);

                // 判断是否被系统自动删除
                if (evicted) {

                    // 将删除的图片存入软引用中
                    SoftReference<Bitmap> bitmapSoftReference = new SoftReference<Bitmap>(
                            oldValue);
                    map.put(key, bitmapSoftReference);
                    Log.i(TAG, "saveBitmap: 往SoftReference中存数据!!!!");
                }
            }

            /**
             * 参数代表要新加入LruCache的键值对
             *
             * @param key
             *            加入数据的键
             * @param value
             *            加入数据的值
             * @return 一般返回新加入的数据(图片)的大小
             */
            @Override
            protected int sizeOf(String key, Bitmap value) {

                return value.getByteCount();
            }
        };
    }

    private MyLruCache myLru;

    public synchronized MyLruCache getLruInstence() {
        if (myLru == null) {
            // 创建一个容量为3的线程池
            myLru = new MyLruCache();
            return myLru;
        }
        return myLru;

    }

    public Executor getInstence() {
        if (executor == null) {
            // 创建一个容量为3的线程池
            executor = Executors.newFixedThreadPool(3);
            return executor;
        }
        return executor;

    }

    // 从缓存中获取bitmap对象的方法
    public Bitmap getBitmap(String url) {

        // 首先是通过对应网址为键从运行时内存中找
        Bitmap result = lrucache.get(url);
        // 若有会返回

        if (result == null) {
            // 若为空再从软引用中找
            SoftReference<Bitmap> soft = map.get(url);
            if (soft != null) {
                // 若存在则result获取对应bitmap对象
                result = soft.get();
                Log.i(TAG, "getBitmap:SoftReference中取出数据: ");
            } else {
                // 若没有 再从本地内存卡中读取
                result = BitmapFactory.decodeFile(path + "/"
                        + url.replace("/", "_"));
                if (result != null) {
                    Log.i(TAG, "getBitmap:从内存卡中取出数据");
                }

                // 若都没有证明图片从未下载过

            }
        } else {
            Log.i(TAG, "getBitmap:lrucache中取出数据");
        }

        // 将得到的bitmap对象返回
        return result;
    }

    // 存储图片的方法将图片网址作为键bitmap对象为值
    public void saveBitmap(String url, Bitmap bitmap) {
        // 首先是存入到强引用lrucache中,lrucache本身可以存储多组键值对
        lrucache.put(url, bitmap);

        Log.i(TAG, "saveBitmap: 往lruCache中存储数据!!!!");

        // 由于网址对应的 / 会造成多级文件夹所以替换为 _
        String urlName = url.replace("/", "_");

        // 创建文件夹
        File file = new File(path);

        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            // 之后也要将下载好的图片通过io流存入到本地的内存卡中
            // 以便于当程序关闭之后再次开启时能从本地读取图片
            FileOutputStream fos = new FileOutputStream(file + "/" + urlName);

            // 将bitmap对象压缩存入对应路径的内存卡中
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            Log.i(TAG, "saveBitmap: 往内存卡中存储数据!!!!");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值