在Android中清除Glide图像缓存

3

我正在使用Glide库来显示图片。我正在尝试使用以下代码清除Glide缓存:

Glide.get(MainActivity.this).clearDiskCache();
Glide.get(MainActivity.this).clearMemory();

以上代码在Async中,我在Do background部分调用它。

当我运行此代码时,总是出现:

Caused by: java.lang.IllegalArgumentException: You must call this method on the main thread
at com.bumptech.glide.util.Util.assertMainThread(Util.java:135)
at com.bumptech.glide.Glide.clearMemory(Glide.java:370)

这里可能出了什么问题?如果我在MainThread中使用上述代码,它会要求在后台运行。

我该如何清除缓存?

谢谢!


1
你尝试在 onPostExecute() 中执行 Glide.get(MainActivity.this).clearMemory(); 吗? - Much Overflow
不,我会尝试的。 - Sanjana Nair
@MuchOverflow:那就是问题所在。抱歉,是我的错!无论如何还是谢谢你! - Sanjana Nair
1个回答

10

clearMemory() 必须在主线程上调用。

clearDiskCache() 必须在后台线程上调用。

不能在同一个线程上同时调用两个方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接