如何使用Volley清除特定NetworkImageView的缓存?

时间:2016-01-11 16:37:30

标签: android caching android-volley networkimageview

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

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

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

1 个答案:

答案 0 :(得分:0)

您可以在VolleySingleton class:

中尝试以下代码
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 null。

或者将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;
}

希望它可以帮到你!