在展示图片的时候,我们不仅仅需要把图片展示出来,往往还需要把图片下载到本地,这里举例取出Glide的GIF图片放到指定的文件目录,之前写过一篇如何加载GIF:
取出Glide中文件的地址:
/**
* Glide 获得图片缓存路径
*/
private String getImagePath(String imgUrl) {
String path = null;
FutureTarget<File> future = Glide.with(this)
.load(imgUrl)
.downloadOnly(500,500);
try {
File cacheFile = future.get();
path = cacheFile.getAbsolutePath();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return path;
}
如果你直接在主线程取缓存路径的话会报错:
java.lang.IllegalArgumentException: YOu must call this method on a background thread
说明这个必须在子线程去操作。
权限:
<