Android使用磁盘缓存DiskLruCache

本文已同步发表于我的微信公众号,搜索 代码说 即可关注,欢迎与我沟通交流。

写在前面

DiskLruCache 不同于LruCache,LruCache是将数据缓存到内存中去,而DiskLruCache是外部缓存,例如可以将网络下载的图片永久的缓存到手机外部存储中去,并可以将缓存数据取出来使用,DiskLruCache不是google官方所写,但是得到了官方推荐,DiskLruCache没有编写到SDK中去,如需使用可直接copy这个类到项目中去。

DiskLruCache地址:
https://android.googlesource.com/platform/libcore/+/android-4.1.1_r1/luni/src/main/java/libcore/io/DiskLruCache.java

或者可以在Jake大神的Github上找到:
https://github.com/JakeWharton/DiskLruCache

DiskLruCache常用方法

方法备注
DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)打开一个缓存目录,如果没有则首先创建它,directory:指定数据缓存地址 appVersion:APP版本号,当版本号改变时,缓存数据会被清除 valueCount:同一个key可以对应多少文件 maxSize:最大可以缓存的数据量
Editor edit(String key)通过key可以获得一个DiskLruCache.Editor,通过Editor可以得到一个输出流,进而缓存到本地存储上
void flush()强制缓冲文件保存到文件系统
Snapshot get(String key)通过key值来获得一个Snapshot,如果Snapshot存在,则移动到LRU队列的头部来,通过Snapshot可以得到一个输入流InputStream
long size()缓存数据的大小,单位是byte
boolean remove(String key)根据key值来删除对应的数据,如果该数据正在被编辑,则不能删除
void delete()关闭缓存并且删除目录下所有的缓存数据,即使有的数据不是由DiskLruCache 缓存到本目录的
void close()关闭DiskLruCache,缓存数据会保留在外存中
boolean isClosed()判断DiskLruCache是否关闭,返回true表示已关闭
File getDirectory()缓存数据的目录

如何使用DiskLruCache

1、因为要操作外部存储,所以必须要先加上权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

另外要从网络下载图片,还要加上权限:

 <uses-permission android:name="android.permission.INTERNET" />

2、DiskLruCache是在外部存储上(如SD卡),所以首先判断外部存储是否存在:

  /**
   * Get a usable cache directory (external if available, internal otherwise).
   * external:如:/storage/emulated/0/Android/data/package_name/cache
   * internal 如:/data/data/package_name/cache
   *
   * @param context    The context to use
   * @param uniqueName A unique directory name to append to the cache dir
   * @return The cache dir
   */
  public static File getDiskCacheDir(Context context, String uniqueName) {
      final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||!isExternalStorageRemovable() 
              ? context.getExternalCacheDir().getPath()
              : context.getCacheDir().getPath();
      return new File(cachePath + File.separator + uniqueName);
  }

(1)、首先判断外部缓存是否被移除或已存满,如果已存满或者外存储被移除,则缓存目录=context.getCacheDir().getPath(),即存到 /data/data/package_name/cache 这个文件系统目录下;
(2)、反之缓存目录=context.getExternalCacheDir().getPath(),即存到 /storage/emulated/0/Android/data/package_name/cache 这个外部存储目录中,PS:外部存储可以分为两种:一种如上面这种路径 (/storage/emulated/0/Android/data/package_name/cache), 当应用卸载后,存储数据也会被删除,另外一种是永久存储,即使应用被卸载,存储的数据依然存在,存储路径如:/storage/emulated/0/mDiskCache,可以通过Environment.getExternalStorageDirectory().getAbsolutePath() + “/mDiskCache” 来获得路径。

3、根据URL下载一个在线图片并把它写到输出流outputstream中:

  /**
     * Download a bitmap from a URL and write the content to an output stream.
     *
     * @param urlString The URL to fetch
     * @return true if successful, false otherwise
     */
    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(), IO_BUFFER_SIZE);
            out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE);
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Error in downloadBitmap - " + e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
            }
        }
        return false;
    }

4、上面已经下载了图片,接着初始化DiskLruCache,并使用DiskLruCache.Editor准备缓存:

  private static final int MAX_SIZE = 10 * 1024 * 1024;//10MB
    private DiskLruCache diskLruCache;
    private void initDiskLruCache() {
        if (diskLruCache == null || diskLruCache.isClosed()) {
            try {
                File cacheDir = CacheUtil.getDiskCacheDir(this, "CacheDir");
                if (!cacheDir.exists()) {
                    cacheDir.mkdirs();
                }
                //初始化DiskLruCache
                diskLruCache = DiskLruCache.open(cacheDir, 1, 1, MAX_SIZE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

下载图片时需要放在异步线程里,这里放在了AsyncTask的doInBackground中:

@Override
protected Boolean doInBackground(Object... params) {
    try {
        String key = Util.hashKeyForDisk(Util.IMG_URL);
        DiskLruCache diskLruCache = (DiskLruCache) params[0];
         //得到DiskLruCache.Editor
        DiskLruCache.Editor editor = diskLruCache.edit(key);
        if (editor != null) {
            OutputStream outputStream = editor.newOutputStream(0);
            if (downloadUrlToStream(Util.IMG_URL, outputStream)) {
                publishProgress("");
                //写入缓存
                editor.commit();
            } else {
                 //写入失败
                editor.abort();
            }
        }
        diskLruCache.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

上面代码中有个hashKeyForDisk()方法,其作用是把图片URL经过MD5加密生成唯一的key值,避免了URL中可能含有非法字符问题,hashKeyForDisk()代码如下:

 /**
  * A hashing method that changes a string (like a URL) into a hash suitable for using as a
  * disk filename.
  */
 public static String hashKeyForDisk(String key) {
     String cacheKey;
     try {
         final MessageDigest mDigest = MessageDigest.getInstance("MD5");
         mDigest.update(key.getBytes());
         cacheKey = bytesToHexString(mDigest.digest());
     } catch (NoSuchAlgorithmException e) {
         cacheKey = String.valueOf(key.hashCode());
     }
     return cacheKey;
 }

 private static String bytesToHexString(byte[] bytes) {
     // http://stackoverflow.com/questions/332079
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < bytes.length; i++) {
         String hex = Integer.toHexString(0xFF & bytes[i]);
         if (hex.length() == 1) {
             sb.append('0');
         }
         sb.append(hex);
     }
     return sb.toString();
 }

经过上面的代码,我们已经可以看到图片已经缓存到 /storage/emulated/0/Android/data/package_name/cache/CacheDir 这个目录下了:
cacheDir.png

第一个标识为110.78kb大小的就是我们缓存下来的图片,它的名字正是由图片的URL经过MD5加密得到的,它下面的journal文件是用来记录的,来看里面的内容:

journal.png

  • 第一行:libcore.io.DiskLruCache固定写死
  • 第二行:DiskLruCache版本号
  • 第三行:APP版本号,由open()方法的参数appVersion传入
  • 第四行:同一个key可以对应多少文件,由open()方法的参数valueCount传入,一般为1
  • 第五行:空格
  • 第六行:以DIRTY开头,后面跟着的是图片的key值,表示准备缓存这张图片,当调用DiskLruCache的edit()时就会生成这行记录
  • 第七行: 以CLEAN开头,后面跟着的是图片的Key值和大小,当调用editor.commit()时会生成这条记录,表示缓存成功;如果调用editor.abort()表示缓存失败,则会生成REMOVE开头的表示删除这条数据。

5、通过diskLruCache.get(key)得到DiskLruCache.Snapshot,key是经过MD5加密后那个唯一的key,接着使用Snapshot.getInputStream()可以得到输入流InputStream ,进而得到缓存图片:

private Bitmap getCache() {
     try {
         String key = Util.hashKeyForDisk(Util.IMG_URL);
         DiskLruCache.Snapshot snapshot = diskLruCache.get(key);
         if (snapshot != null) {
             InputStream in = snapshot.getInputStream(0);
             return BitmapFactory.decodeStream(in);
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
     return null;
 }
Bitmap bitmap = getCache();
     if (bitmap != null) {
         iv_img.setImageBitmap(bitmap);
     }

效果图:
cache.png

完整Demo地址:
http://download.csdn.net/detail/u013700502/9883037

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android中头像本地缓存可以通过使用SharedPreferences或者使用磁盘缓存的方式实现。 1. 使用SharedPreferences缓存头像: 可以将头像图片转化成Base64编码的字符串,然后将其保存在SharedPreferences中。当需要加载头像时,从SharedPreferences中读取Base64字符串,再将其转化为Bitmap对象。 具体实现代码如下: ``` //保存头像到SharedPreferences中 public void saveAvatarToSharedPreferences(Bitmap bitmap) { SharedPreferences sharedPreferences = getSharedPreferences("avatar", MODE_PRIVATE); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); String avatarBase64 = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("avatar", avatarBase64); editor.apply(); } //从SharedPreferences中加载头像 public Bitmap loadAvatarFromSharedPreferences() { SharedPreferences sharedPreferences = getSharedPreferences("avatar", MODE_PRIVATE); String avatarBase64 = sharedPreferences.getString("avatar", null); if (avatarBase64 != null) { byte[] bytes = Base64.decode(avatarBase64, Base64.DEFAULT); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } return null; } ``` 2. 使用磁盘缓存方式缓存头像: 可以使用Android中的LruCache或者DiskLruCache类来实现磁盘缓存头像。LruCache是一种内存缓存,而DiskLruCache则是一种磁盘缓存。 具体实现代码如下: ``` //初始化DiskLruCache private void initDiskLruCache() throws IOException { File cacheDir = getExternalCacheDir(); if (cacheDir != null) { int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; diskLruCache = DiskLruCache.open(cacheDir, versionCode, 1, 10 * 1024 * 1024); } } //保存头像到DiskLruCache中 public void saveAvatarToDiskLruCache(String key, Bitmap bitmap) throws IOException { if (diskLruCache == null) { initDiskLruCache(); } if (diskLruCache != null) { DiskLruCache.Editor editor = diskLruCache.edit(key); if (editor != null) { OutputStream outputStream = editor.newOutputStream(0); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); editor.commit(); } } } //从DiskLruCache中加载头像 public Bitmap loadAvatarFromDiskLruCache(String key) throws IOException { if (diskLruCache == null) { initDiskLruCache(); } if (diskLruCache != null) { DiskLruCache.Snapshot snapshot = diskLruCache.get(key); if (snapshot != null) { InputStream inputStream = snapshot.getInputStream(0); return BitmapFactory.decodeStream(inputStream); } } return null; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小马快跑_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值