【发布时间】:2014-08-13 13:10:20
【问题描述】:
有人知道是否可以使用 Google 的 Volley 库从单个 NetworkImageView 中清除缓存的图像吗?
我在 NetworkImageView 中有一个头像图像,我想在将新图像上传到服务器后显示它。目前如果我这样做
profileImg.setImageUrl("myUrl", mImageLoader);我得到了缓存的图片。
【问题讨论】:
有人知道是否可以使用 Google 的 Volley 库从单个 NetworkImageView 中清除缓存的图像吗?
我在 NetworkImageView 中有一个头像图像,我想在将新图像上传到服务器后显示它。目前如果我这样做
profileImg.setImageUrl("myUrl", mImageLoader);我得到了缓存的图片。
【问题讨论】:
看看这个:
1) 关闭缓存: 如果要禁用特定 url 的缓存,可以使用 setShouldCache() 方法,如下所示。
StringRequest stringReq = new StringRequest(....);
stringReq.setShouldCache(false);
2) 删除特定 URL 的缓存:使用 remove() 删除 URL 的缓存。
yourRequestQueue.getCache().remove(url);
3) 删除所有缓存:
yourRequestQueue.getCache().clear(url);
另外,请查看此链接here。
希望这会有所帮助。
【讨论】:
所以迟来的回答我自己的问题。我最终采取了不同的方法来确保我始终获得最新的头像图像副本,结果证明它非常简单。
我没有尝试从缓存中清除单个图像,而是使用以下方法。
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,因此我总是得到该图像的最新版本。
这种方法的美妙之处在于它不仅限于 Volley,而且您可以选择使用任何方式进行网络调用。
【讨论】:
在 LruBitmapCache 你应该做 diable put(url, bitmap) :
@Override
public void putBitmap(String url, Bitmap bitmap) {
// put(url, bitmap);
}
这样每次调用 volley 方法时图片不要保存捕捉
【讨论】:
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/
【讨论】:
如果你像这样创建缓存:
RequestQueue requests = Volley.newRequestQueue( context );
BitmapCache cache = new BitmapCache(cacheSize);
ImageLoader loader = new ImageLoader( requests, cache );
你可以这样清除:
public void clear () {
cache.evictAll();
requests.getCache().clear();
}
【讨论】:
我创建了 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);
我都试过了,但是 Cashe 没有被删除
【讨论】: