1. 程式人生 > >Android glide 圖片從快取拿

Android glide 圖片從快取拿

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

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

        @Override
        protected File doInBackground(String... params) {
            String imgUrl =  params[0];
            try {
                return Glide.with(context)
                        .load(imgUrl)
                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
                        .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();

            Bitmap bmp = BitmapFactory.decodeFile(path);
            list.add(bmp);
        }
    }