我正在尝试在我的应用程序中的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);
答案 0 :(得分:2)
您可以尝试以下内容(在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;
}
希望它有所帮助!
答案 1 :(得分:0)
Google Volley提供了两种方法来清除缓存中的项目:
AppController.getInstance().getRequestQueue().getCache().remove(key);
和
AppController.getInstance().getRequestQueue().getCache().invalidate(key, fullExpire);
remove()表示您要删除实际的缓存数据。
invalidate()表示您只是将数据标记为无效。因此,凌空将检查服务器数据是否仍然有效。完全过期确定是否在凌空使用服务器验证数据之前使用数据。
要在每30分钟清除缓存,请使用以下代码: -
您可以使用凌空的serverDate 来获取最初收到回复的日期
AppController.getInstance().getRequestQueue().getCache().get(url).serverDate
因此在您的代码中使用getMinutesDifference函数作为
public static long getMinutesDifference(long timeStart,long timeStop){
long diff = timeStop - timeStart;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
将该功能称为
Calendar calendar = Calendar.getInstance();
long serverDate = AppController.getInstance().getRequestQueue().getCache().get(url).serverDate;
if(getMinutesDifference(serverDate, calendar.getTimeInMillis()) >=30){
AppController.getInstance().getRequestQueue().getCache().invalidate(url, true);
}
如果之前的网址响应&gt; = 30分钟,它将使缓存无效。
希望它有所帮助!