Android 禁用或删除NetworkImageView中的缓存-截取

Android 禁用或删除NetworkImageView中的缓存-截取,android,caching,android-volley,Android,Caching,Android Volley,我正在尝试禁用NetworkImageView中的缓存,我的应用程序中的哪个截击类。我尝试了这段代码,但它没有删除缓存 mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView); mImageLoader = VolleySingleton.getInstance().getImageLoader(); mNetworkImageView.setImageUrl(IMAGE_URL,

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

mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance().getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
VolleySingleton.getInstance().getRequestQueue().getCache().remove(IMAGE_URL);
您可以尝试以下方法(在
截击单打
类中):

调试时可以检查,在
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;
}
公共ImageContainer get(字符串requestUrl、ImageListener、ImageListener、, int maxWidth、int maxHeight、ScaleType(ScaleType){ //仅满足从主线程启动的请求。 throwIfNotOnMainThread(); 最终字符串cacheKey=getCacheKey(requestUrl、maxWidth、maxHeight、scaleType); //尝试在远程映像的缓存中查找请求。 位图cachedBitmap=mCache.getBitmap(cacheKey); if(cachedBitmap!=null){ w(“cachedBitmap”,“位图缓存!”); //返回缓存的位图。 ImageContainer=newImageContainer(cachedBitmap,requestUrl,null,null); onResponse(容器,true); 返回容器; } //缓存中不存在位图,请获取它! 图像容器图像容器= 新的ImageContainer(null、requestUrl、cacheKey、imageListener); //更新调用者,让他们知道他们应该使用默认位图。 onResponse(imageContainer,true); //检查请求是否已在进行中。 BatcheImage请求=mInFlightRequests.get(cacheKey); if(请求!=null){ //如果是,请将此请求添加到侦听器列表中。 request.addContainer(imageContainer); 返回图像容器; } //请求尚未发送。请将新请求发送到网络并重试 //跟踪它。 Request newRequest=makeImageRequest(requestUrl、maxWidth、maxHeight、scaleType、, 缓存密钥); mRequestQueue.add(newRequest); mInFlightRequests.put(cacheKey, 新的BatcheImage请求(newRequest,imageContainer)); 返回图像容器; }

希望有帮助

Google Volley提供了两种从缓存中清除项目的方法:

AppController.getInstance().getRequestQueue().getCache().remove(key);

remove()表示您正在删除实际缓存的数据

invalidate()表示您只是将数据标记为无效。因此,volley将与服务器检查数据是否仍然有效。“完全过期”决定是否在volley向服务器验证数据之前使用该数据

要在每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);
}
并将函数调用为

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);
}
如果上一个url响应>=30分钟,它将使缓存无效


希望有帮助

这对我不起作用。甚至我也尝试创建新的请求