android Glide 獲取磁盤緩存

Glide是Google推薦的圖片加載庫, 加載圖片一般以下面的形式:

Glide.with(context).load(ImgUrl)
      .asBitmap()
      .error(R.drawable.error)
      .placeholder(R.drawable.loading)
      .dontAnimate()
      .diskCacheStrategy(DiskCacheStrategy.ALL)
      .into(ImageView);

load對象: String(文件路徑,網絡地址),File(文件資源),Integer(資源id);
asGif:表示的gif動畫,asBitmap:表示靜態圖
diskCacheStrategy磁盤緩存策略:
(1). DiskCacheStrategy.RESULT:展示小大的圖片緩存
(2). DiskCacheStrategy.ALL; 展示在控件中大小圖片尺寸和原圖都會緩存
(3). DiskCacheStrategy.NONE:不設置緩存
(4). DiskCacheStrategy.SOURCE:原圖緩存

placeholder(R.drawable.loading): 目標從加載到展示時的控件的顯示狀態(多用網絡加載動畫)
error(R,drawable,error): 加載失敗時,控件顯示的圖片。
into(ImageView): 展示的控件

Glide加載網絡圖片時, 會將圖片緩存一份到本地, 下次再加載的時候優先從緩存中拿取, 提高速度.

如果想獲取Glide緩存的圖片, 怎麼辦?
我們都知道 開源框架Universal_Image_Loader 提供 “getDiskCache().get(url)” 去獲取圖片的本地緩存.
但是Glide並沒有提供類似方法.

那麼Glide如何獲取本地的緩存 ?
Glide提供 downloadOnly() 接口, 可以通過此接口來獲取緩存文件. 但是有一個前提:
使用Glide加載圖片時, 必須指定diskCacheStrategy磁盤緩存策略爲DiskCacheStrategy.ALL 或 DiskCacheStrategy.SOURCE

使用downloadOnly()獲取緩存文件(此方法需要在線程中執行, 如果確定文件有緩存, 它會從緩存中讀取文件, 很快):

private class GetImageCacheTask extends AsyncTask<String, Void, File> {
        private final Context context;

        public SaveImageTask(Context context) {
            this.context = context;
        }

        @Override
        protected File doInBackground(String... params) {
            String imgUrl =  params[0];
            try {
                return Glide.with(context)
                        .load(imgUrl)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get();
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(File result) {
            if (result == null) {
                return;
            }
            //此path就是對應文件的緩存路徑
            String path = result.getPath();
            //將緩存文件copy, 命名爲圖片格式文件
            copyFile(path, newPath);
        }
    }

上面的path就是對應圖片的緩存地址, 類似於: /data/data/包名/cache/image_manager_disk_cache/6872faf4075a6461f3d7ceb2e5ff625beeaae67d3b7e44a0d1e3cd332aa471dc.0

Glide對文件緩存時, 採用SHA-256加密算法, 所以如果需要獲得圖片, 需要將獲得的文件copy一份, 命名爲圖片格式的文件.
對應代碼:

    /**
    * oldPath: 圖片緩存的路徑
    * newPath: 圖片緩存copy的路徑
    */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int byteRead;
            File oldFile = new File(oldPath);
            if (oldFile.exists()) {
                InputStream inStream = new FileInputStream(oldPath);
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024];
                while ( (byteRead = inStream.read(buffer)) != -1) {
                    fs.write(buffer, 0, byteRead);
                }
                inStream.close();
            }
        }
        catch (Exception e) {
            System.out.println("複製文件操作出錯");
            e.printStackTrace();
        }
    }

這樣就會在新的路徑下生成所需要的圖片文件. 通過這種方式就可以獲取Glide的磁盤緩存.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章