【无标题】

写了一个 diskLRUcache 工具类,

public class DiskLruCacheUtil {
    static DiskLruCacheUtil singleton;
    private String mUrl;
    private DiskLruCache mDiskLruCache;

    //防止用户可以创建该对象
    private DiskLruCacheUtil(DiskLruCacheUtil.Builder builder) {

        File file = DiskCacheUtils.getDiskCacheDir(builder.mContext, "test_cache");
        if (!file.exists()) {
            file.delete();
            file.mkdirs();
        }
        mDiskLruCache = DiskLruCache.create(FileSystem.SYSTEM, file, DiskCacheUtils.getAppVersion(builder.mContext), 1, 10 * 1024 * 1024);


    }

    public static DiskLruCacheUtil with(Context context) {
        if (singleton == null) {
            synchronized (RxImageLoader.class) {
                if (singleton == null) {
                    singleton = new Builder(context).build();
                }
            }
        }
        return singleton;
    }

    public static class Builder {

        private Context mContext;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        public DiskLruCacheUtil build() {
            return new DiskLruCacheUtil(this);
        }
    }

    public DiskLruCacheUtil load(String url) {
        this.mUrl = url;
        return singleton;
    }


    public void putDataToCache(InputStream inputStream) {
        String key = DiskCacheUtils.getMD5String(mUrl);
        DiskLruCache.Editor editor = null;
        try {
            editor = mDiskLruCache.edit(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Sink sink = editor.newSink(0);
        try {
            //long fileSize = body.contentLength();
            //long fileSizeDownloaded = 0;
            byte[] fileReader = new byte[4096];
            Buffer buffer = new Buffer();
            while (true) {
                int read = inputStream.read(fileReader);
                if (read == -1) {
                    break;
                }
                buffer.write(fileReader, 0, read);
                //fileSizeDownloaded += read;
                sink.write(buffer, read);
                buffer.clear();
            }
            if (buffer != null) {
                buffer.clear();
                buffer.close();
            }

            if (inputStream != null) {
                inputStream.close();
            }

            if (sink != null) {
                sink.flush();
                sink.close();
            }
            editor.commit();

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

        }

    }

    public Bitmap getDataFromDiskLruCache(String url) {
        Bitmap bitmap = null;
        try {
            final String key = DiskCacheUtils.getMD5String(url);
            DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
            if (snapshot == null) return null;
            //获取资源的输出流,Source类似InputStream
            Source source = snapshot.getSource(0);
            Buffer buffer = new Buffer();
            //读取4*1024数据放入buffer中并返回读取到的数据的字节长度
            long ret = source.read(buffer, 4 * 1024);
            //判断文件是否读完
            while (ret != -1) {
                ret = source.read(buffer, 4 * 1024);
            }
            //获取到buffer的inputStream对象
            InputStream inputStream = buffer.inputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            if (inputStream != null) {
                inputStream.close();
            }
            if (buffer != null) {
                buffer.clear();
                buffer.close();
            }
            if (source != null) {
                source.close();
            }
            if (snapshot != null) {
                snapshot.close();
            }
            Log.e("取到照片", "bitmap");
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }


        return bitmap;
    }
}

使用的时候直接一句引用:

 DiskLruCacheUtil.with(Test2Activity.this).load(url).putDataToCache(responseBody.byteStream());
  Bitmap bitmap = DiskLruCacheUtil.with(Test2Activity.this).getDataFromDiskLruCache(url)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值