2016-01-11 2 views
1

У меня есть NetworkImageView в моем приложении, и ссылка в базе данных меняется каждые пять секунд, поэтому мне нужно удалить кеш для обновления изображения. Я пробовал этот код, но он не удаляет кеш.Как очистить кеш для конкретного NetworkImageView с помощью Volley?

VolleySingleton.getInstance().getRequestQueue().getCache().remove(IMAGE_URL); 

mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView); 
mImageLoader = VolleySingleton.getInstance().getImageLoader(); 
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader); 
+0

Pls прочитал мой ответ на http://stackoverflow.com/questions/34755109/disable-or-remove-cache-in-networkimageview-volley/34757041#34757041 – BNK

+0

@BNK поблагодарить так много, мог бы проверить мой вопрос под ответом, который находится в этой ссылке: http://stackoverflow.com/questions/34734999/android-volley-image-caching-questions/34736194#34736194 также является ответом, который он дал в третьей части хорошего метода? спасибо – shanks

+0

О # 3 в своем ответе, как вы находите в моем ответе, без взлома и без заголовка, вы можете узнать больше о занятиях Volley, касающихся NetworkImageView, таких как ImageLoader ... – BNK

ответ

0

Вы можете попробовать следующий код внутри 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 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; 
} 

Надеется, что это помогает!

 Смежные вопросы

  • Нет связанных вопросов^_^