DiskLruCache学习

本文介绍了如何使用DiskLruCache来缓存从网络获取的图片到本地磁盘,以减轻内存负担。详细讲解了DiskLruCache的工作原理、实例创建过程、缓存路径设置、图片写入与读取的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DiskLruCache认识

可以将从网络获取的图片存储在本地磁盘中,减少内存存储的大小

一般存储位置为:

/sdcard/Android/data/application package/cache

DiskLruCach下载地址
下载


获取DiskLruCach实例获取:

DiskLruCache.open(cacheDir,getAppVersion(this),1,10 * 1024 * 1024);

第一个参数是缓存地址一般就在这个位置/sdcard/Android/data/application package/cache,
第二个参数是app的版本号码(当版本号码改变时所有数据会被清理)
第三个参数是每一个key对应几个缓存文件
第四个参数是缓存的大小

获取缓存路径:

当sd卡存在时使用getExternalCacheDir()方法获取缓存路径( /sdcard/Android/data/application package/cache)
否则使用getCacheDir()(/data/data/application package>cache )


获取缓存路径后需要加上在路径后面拼接上一个字符串uniqueName(为了对不同数据进行区分而设定的唯一值)

写缓存:

private boolean downloadUrlToStream(String urlString, OutputStream outputStream){
        HttpURLConnection urlConnection = null;
        BufferedOutputStream out = null;
        BufferedInputStream in = null;
        try {
            final URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedInputStream(urlConnection.getInputStream(),8 * 1024);
            out = new BufferedOutputStream(outputStream,8 * 1024);
            int b;
            while ((b = in.read()) != -1){
                out.write(b);
            }
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(urlConnection != null){
                urlConnection.disconnect();
            }

            try {
                if(out != null){
                    out.close();
                }
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return false;

    }

DiskLruCache.Editor的实例:

用来写入缓存
DiskLruCache.Editor editor = mDiskLruCache.edit(key);

//下载图片
        new Thread(){
            @Override
            public void run() {
                try {
                    String imageUrl = "http://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1664915200,467145583&fm=80";
                    String key = hashKeyForDisk(imageUrl);
                    DiskLruCache.Editor editor = mDiskLruCache.edit(key);
                    if(editor != null){
                        OutputStream outputStream = editor.newOutputStream(0);
                        if(downloadUrlToStream(imageUrl,outputStream)){
                            editor.commit();//提交写入
                        }else {
                            editor.abort();//放弃本次写入
                        }


                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

获取缓存:

DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);

                        if(snapshot != null){
                            InputStream is = snapshot.getInputStream(0);
                            final Bitmap bitmap = BitmapFactory.decodeStream(is);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    img.setImageBitmap(bitmap);
                                }
                            });

删除

mDiskLruCache.remove(key);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值