【问题标题】:How to clear the cache for specific NetworkImageView with Volley?如何使用 Volley 清除特定 NetworkImageView 的缓存?
【发布时间】:2016-04-16 01:31:02
【问题描述】:

我的应用程序中有一个 NetworkImageView,并且数据库中的链接每五秒更改一次,因此我必须删除缓存以刷新图像。我试过这段代码,但它没有删除缓存。

VolleySingleton.getInstance().getRequestQueue().getCache().remove(IMAGE_URL);

mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance().getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);

【问题讨论】:

  • @BNK 非常感谢你,你能在这个链接的答案下检查我的问题吗:stackoverflow.com/questions/34734999/… 还有,他在第三部分给出的答案是好方法吗?谢谢
  • 关于他的回答中的#3,正如您在我的回答中发现的那样,没有黑客,也没有标题,您可以在与 NetworkImageView 相关的 Volley 类中查看更多信息,例如 ImageLoader...

标签: android caching android-volley networkimageview


【解决方案1】:

您可以在VolleySingleton 类中尝试以下代码:

mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
        @Override
        public Bitmap getBitmap(String url) {
            return null;
        }

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

调试时可以查看,下一行设置断点

Bitmap cachedBitmap = mCache.getBitmap(cacheKey);

ImageLoader.java 中,你会发现cachedBitmap 为空。

或将Log.w("cachedBitmap", "Bitmap cached!"); 作为我的以下代码进行检查:

public ImageContainer get(String requestUrl, ImageListener imageListener,
    int maxWidth, int maxHeight, ScaleType scaleType) {

// only fulfill requests that were initiated from the main thread.
throwIfNotOnMainThread();

final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight, scaleType);

// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
    Log.w("cachedBitmap", "Bitmap cached!");
    // Return the cached bitmap.
    ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
    imageListener.onResponse(container, true);
    return container;
}

// The bitmap did not exist in the cache, fetch it!
ImageContainer imageContainer =
        new ImageContainer(null, requestUrl, cacheKey, imageListener);

// Update the caller to let them know that they should use the default bitmap.
imageListener.onResponse(imageContainer, true);

// Check to see if a request is already in-flight.
BatchedImageRequest request = mInFlightRequests.get(cacheKey);
if (request != null) {
    // If it is, add this request to the list of listeners.
    request.addContainer(imageContainer);
    return imageContainer;
}

// The request is not already in flight. Send the new request to the network and
// track it.
Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType,
        cacheKey);

mRequestQueue.add(newRequest);
mInFlightRequests.put(cacheKey,
        new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2019-03-08
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多