本文介绍了在NetworkImageView-Volley中禁用或删除缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在NetworkImageView中禁用应用程序中哪个Volley类的缓存.我尝试了这段代码,但是它没有删除缓存.

I am trying to disable cache in in the NetworkImageView which Volley class in my app. I tried this code but it does not remove the cache.

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

推荐答案

您可以尝试以下操作(在VolleySingleton类内部):

You can try the following (inside 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) {
        }
    });

您可以在调试时检查,在ImageLoader.java内的Bitmap cachedBitmap = mCache.getBitmap(cacheKey);行设置断点,您会发现cachedBitmap为空.

You can check when debugging, set breakpoint at Bitmap cachedBitmap = mCache.getBitmap(cacheKey); line inside ImageLoader.java, you will find cachedBitmap null.

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

Or put Log.w("cachedBitmap", "Bitmap cached!"); as my following code to check:

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;
}

希望有帮助!

这篇关于在NetworkImageView-Volley中禁用或删除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:35