@Override protected Void doInBackground(String... params) { String imgUrl = params[0]; File file = null; try { FutureTarget<File> future = Glide .with(ImageViewPagerActivity.this) .load(imgUrl) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); file = future.get(); String filePath = file.getAbsolutePath(); String destFileName = System.currentTimeMillis() + FileUtils.getImageFileExt(filePath); File destFile = new File(FileUtils.getDir(""), destFileName); FileUtils.copy(file, destFile);// 保存图片 // 最后通知图库更新 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destFile.getPath())))); } catch (Exception e) { KLog.e(TAG, e.getMessage()); } return null; }
/** * 根据图片的网络地址,拿到使用 Glide 进行缓存后的图片缓存地址 * 注意!!! 该方法要在子线程中调用,否则会出错 * * @param imageUrl 图片的网络地址 * @return 图片的缓存地址 */ public static String getImagePathFromCache(String imageUrl, Context context) { FutureTarget<File> futureTarget = Glide.with(context) .load(imageUrl) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); File cacheFile; try { cacheFile = futureTarget.get(); return cacheFile.getAbsolutePath(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return null; }
@Override public File downLoad(String url, Activity activity) { File file = null; try { file = Glide.with(activity) .load(url) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); Logger.e("下载图片异常" + e.getMessage()); } return file; } };
sliderIv.setLayoutParams(layoutParams); Glide.with(this).load(mUrl).downloadOnly(new SimpleTarget<File>() { @Override public void onResourceReady(File resource, Transition<? super File> transition) {
.with(mContext) .load(url) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .get(); return new DownloadResult(file, url);