搜索一下网络上都是异步获取,唯一一个同步的还是反射,分享一个同步方法.
public static @Nullable File getCacheByUrl(Context context, String url) {
final File[] cacheFile = new File[1];
CountDownLatch countDownLatch = new CountDownLatch(1);
new Thread(() -> {
try {
cacheFile[0] = GlideApp.with(context).downloadOnly().load(url).onlyRetrieveFromCache(true).submit().get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
}).start();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return cacheFile[0];
}