【问题标题】:Volley NetworkImageView clear cached imageVolley NetworkImageView 清除缓存图像
【发布时间】:2014-08-13 13:10:20
【问题描述】:

有人知道是否可以使用 Google 的 Volley 库从单个 NetworkImageView 中清除缓存的图像吗?

我在 NetworkImageView 中有一个头像图像,我想在将新图像上传到服务器后显示它。目前如果我这样做 profileImg.setImageUrl("myUrl", mImageLoader);我得到了缓存的图片。

【问题讨论】:

标签: android android-volley


【解决方案1】:

看看这个:

1) 关闭缓存: 如果要禁用特定 url 的缓存,可以使用 setShouldCache() 方法,如下所示。

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

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

yourRequestQueue.getCache().remove(url);

3) 删除所有缓存

yourRequestQueue.getCache().clear(url);

另外,请查看此链接here

希望这会有所帮助。

【讨论】:

  • 上述方法均不适用于网络图像视图。有什么想法吗?
  • 跟我的情况一样,没用,有人找到解决办法了吗?
  • 查看此链接:stackoverflow.com/questions/25333351/…。检查“Versa”的答案
【解决方案2】:

所以迟来的回答我自己的问题。我最终采取了不同的方法来确保我始终获得最新的头像图像副本,结果证明它非常简单。

我没有尝试从缓存中清除单个图像,而是使用以下方法。

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,而且您可以选择使用任何方式进行网络调用。

【讨论】:

  • 这几乎不是一个解决方案。这意味着您每次想要显示它时都在重新下载该图像,即使它自上次下载以来没有更改。
  • 这是一个在特定情况下有效的解决方案。愿意提供替代方案吗?
  • 当然。只需公开一种从图像缓存中删除键的方法,有一点不同:Volley 将键存储为图像的宽度和高度以及 URL 的组合。因此,您必须删除键 包含 URL 的任何值,而不是删除键字符串。
【解决方案3】:

在 LruBitmapCache 你应该做 diable put(url, bitmap) :

 @Override
    public void putBitmap(String url, Bitmap bitmap) {
       // put(url, bitmap);
    }

这样每次调用 volley 方法时图片不要保存捕捉

【讨论】:

    【解决方案4】:
      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/
    

    【讨论】:

      【解决方案5】:

      如果你像这样创建缓存:

      RequestQueue requests = Volley.newRequestQueue( context );
      BitmapCache cache = new BitmapCache(cacheSize);
      ImageLoader loader = new ImageLoader( requests, cache );
      

      你可以这样清除:

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

      【讨论】:

        【解决方案6】:

        我创建了 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 没有被删除

        【讨论】:

          猜你喜欢
          • 2016-04-16
          • 2016-04-17
          • 1970-01-01
          • 1970-01-01
          • 2013-10-24
          • 2014-03-17
          • 2015-11-11
          • 1970-01-01
          相关资源
          最近更新 更多