android - 禁用或删除 NetworkImageView-Volley 中的缓存

标签 android caching android-volley

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

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

最佳答案

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

    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 空。

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

希望对你有帮助!

关于android - 禁用或删除 NetworkImageView-Volley 中的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755109/

相关文章:

android - 在 Scroll 上获取当前字母?

java - Recyclerview在软键盘出现或消失时向下滚动

CakePHP 3 : find() with cache

c++ - 哪些数据会被缓存?

python - 是否可以将 render_to_response 模板从 django 保存到服务器?

安卓 Volley : Unexpected response code 405

java - 用字符串android中的下划线替换所有句号

android - 子查询中的分组依据

android - 如何使用 volley 库 android 创建圆角图像

android - ArrayAdapter 中的 Volley NetworkImage -> 有条件地加载 Drawable-Res