Volley的NetworkImageView如何清除缓存的图片

8

有没有人知道如何使用谷歌的Volley库清除单个NetworkImageView中缓存的图像?

我在一个NetworkImageView中有一个头像图像,并希望在将其上传到服务器后显示新图像。目前,如果执行profileImg.setImageUrl("myUrl", mImageLoader);,会显示缓存的图像。


请查看:https://dev59.com/EmQm5IYBdhLWcg3w6SlR - Siddharth_Vyas
6个回答

5

请看这个:

1)关闭缓存: 如果你想禁用某个URL的缓存,你可以使用以下setShouldCache()方法。

StringRequest stringReq = new StringRequest(....);
stringReq.setShouldCache(false);

2) 删除特定URL的缓存: 使用remove()删除URL的缓存。

yourRequestQueue.getCache().remove(url);

3) 清除所有缓存

yourRequestQueue.getCache().clear(url);

另外,看看这个链接在这里

希望这可以帮到你。


以上的方法都不能在网络ImageView上工作。有什么建议吗? - Prashanth Debbadwar
与我情况相同,没有任何作用,有人找到解决方法吗? - UMAR-MOBITSOLUTIONS
请查看此链接:https://dev59.com/rF8e5IYBdhLWcg3w0NGO。请查看“Versa”的答案。 - Siddharth_Vyas

3

很抱歉,我来晚了。为确保我始终获取最新的头像图像,我采用了不同的方法,这种方法非常简单。

而不是尝试清除缓存中的个别图像,我使用以下方法。

int time = (int) (System.currentTimeMillis());//gets the current time in milliseconds
String myUrl = "www.mySite.com?timestamp=" + String.ValueOf(time);
profileImg.setImageUrl("myUrl", mImageLoader);

这个做法是在我调用带有当前时间戳的url获取图片时,向url附加一个虚构参数。这确保了我一直在调用唯一的url,因此能够获得该图片最新版本。
这种方法的优点在于它不仅限于Volley,在您进行网络调用时可以随意使用。

这几乎不是一个解决方案。这意味着每次想要显示图像时,您都会重新下载它,即使自上次下载以来它没有发生变化。 - Eran Goldin
这是在特定情况下能够工作的解决方案。是否能提供另一种选择? - Ivan Wooll
当然可以。只需暴露一个从图像缓存中删除键的方法,但有一个小技巧:Volley将键存储为图像的宽度和高度与URL的组合。因此,您不必删除键字符串,而是必须删除任何其键包含URL的值。 - Eran Goldin

1
在 LruBitmapCache 中,您应该禁用 put(url, bitmap) 方法:
 @Override
    public void putBitmap(String url, Bitmap bitmap) {
       // put(url, bitmap);
    }

通过这种方式,每次调用Volley方法时,图像不会保存到缓存中。

0
如果您像这样创建缓存:
RequestQueue requests = Volley.newRequestQueue( context );
BitmapCache cache = new BitmapCache(cacheSize);
ImageLoader loader = new ImageLoader( requests, cache );

您可以这样清除所有内容:

public void clear () {
    cache.evictAll();
    requests.getCache().clear();
}

0
我创建了volleySingleton类。我使用ImageLoader加载图像。我需要删除缓存,但无法做到。我尝试了以下三种方法:
1)删除特定URL的缓存:使用remove()删除URL的缓存。
VolleySingleton.getInstance().getRequestQueue().getCache().remove(ImageUrl);
2)删除所有缓存:

VolleySingleton.getInstance().getRequestQueue().getCache().clear();

3) VolleySingleton.getInstance().getRequestQueue().getCache().invalidate(ImageUrl,true);

我已经尝试了所有方法,但缓存仍未被删除。


0
  To turn off the cache:

1
request.setShouldCache(false);
to remove the cache for a specific request:

1
queue.getCache().remove(url);
to clear all cache:

1
queue.getCache().clear();
to invalidate the cache: this will allow to display the cached data until the response is received. When the response is received, it will automatically override the cached data.

1
queue.getCache().invalidate(url, true);

    for more details you can refer to the 
    url:http://androidresearch.wordpress.com/2014/02/01/android-volley-tutorial/

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