gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:04:41 25 4
gpt4 key购买 nike

我试图在我的应用程序中的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com