2014-05-05 89 views
5

我目前正在研究一個前端應用程序,它嚴重依賴於從web服務下載的圖像。爲了保持簡短,前端從我們的webservice下載了具有3個圖像插槽的給定用戶配置文件。如果這樣的用戶上傳一個新的圖片(保持相同的網址),它不會刷新,直到我殺了我的應用程序,所以如果我把它放在後臺,恢復數據將不會改變。有什麼辦法可以強制Volley刷新圖像嗎?

下面是與ImageNetworkView交易的代碼的小樣本:

mImage = (NetworkImageView) mView 
      .findViewById(R.id.profile_network_image); 

    mProgressBar = (ProgressBar) mView 
      .findViewById(R.id.profileProgressBar); 

    // Cargo image loader 
    mLoader = VolleySingleton.getInstance(mActivity).getImageLoader(); 

    if(mLoader!=null && imageUrl!=null){ 
     mLoader.get(imageUrl, new ImageListener() { 


      @Override 
      public void onErrorResponse(VolleyError error) { 

       System.out.println("Error loading "+imageUrl+"..."); 

      } 

      @Override 
      public void onResponse(ImageContainer response, boolean isImmediate) { 
       if (response.getBitmap() != null) { 
        mImage.setImageBitmap(response.getBitmap()); 
        mProgressBar.setVisibility(View.GONE); 
        System.out.println("Done loading "+imageUrl); 
       } 

      } 
     }); 

mImage.setImageUrl(imageUrl, mLoader); 

這裏是我的兩個VolleySingleton和Cache類:

public class VolleySingleton { 
private static VolleySingleton mInstance = null; 
private RequestQueue mRequestQueue; 
private ImageLoader mImageLoader; 
private ImageLoader.ImageCache mImageCache; 

private VolleySingleton(Context context){ 
    mRequestQueue = Volley.newRequestQueue(context); 
    mImageCache = new BitmapLruImageCache(); 
    mImageLoader = new ImageLoader(this.mRequestQueue, mImageCache); 
} 

public static VolleySingleton getInstance(Context context){ 
    if(mInstance == null){ 
     mInstance = new VolleySingleton(context); 
    } 
    return mInstance; 
} 

public RequestQueue getRequestQueue(){ 
    return this.mRequestQueue; 
} 

public ImageLoader getImageLoader(){ 
    return this.mImageLoader; 
} 

public ImageLoader.ImageCache getImageCache(){ 
    return mImageCache; 
} 

Cache類:

public class BitmapLruImageCache extends LruCache<String, Bitmap> implements ImageCache { 

public static int getDefaultLruCacheSize() { 
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 
    final int cacheSize = maxMemory/8; 

    return cacheSize; 
} 

public BitmapLruImageCache() { 
    this(getDefaultLruCacheSize()); 

    Log.v("tag", Integer.toString(getDefaultLruCacheSize())); 
} 

public BitmapLruImageCache(int sizeInKiloBytes) { 
    super(sizeInKiloBytes); 

} 

@Override 
protected int sizeOf(String key, Bitmap value) { 
    return value.getRowBytes() * value.getHeight()/1024; 
} 

@Override 
public Bitmap getBitmap(String url) { 
    return get(url); 
} 

@Override 
public void putBitmap(String url, Bitmap bitmap) { 
    put(url, bitmap); 
} 

謝謝大家!

編輯:我一直試圖找到一個解決辦法,到目前爲止,我想出了這個:

如果我下分配給圖像URL當我打電話getBitmap緩存中的新形象(url)我會得到新的圖像,但問題仍然存在,如果我用後退按鈕關閉應用程序,並再次打開它凌空加載舊圖像,但如果我殺了應用程序,它會加載新的.. 。這裏是我用來將圖像放在緩存中的代碼:

VolleySingleton.getInstance(mActivity).getImageCache().putBitmap(imageUrl, bmp); 

tempBitmap = VolleySingleton.getInstance(mActivity).getImageCache().getBitmap(imageUrl); 

回答

0

事情是圖像緩存在內存中,在撥打電話之前,ImageLoader會進行檢查。

 // Try to look up the request in the cache of remote images. 
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey); 
    if (cachedBitmap != null) { 
     // Return the cached bitmap. 
     ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null); 
     imageListener.onResponse(container, true); 
     return container; 
    } 

您將需要通過手動清理緩存:

VolleySingleton.getInstance(mActivity).getImageCache().remove(imageUrl); 

VolleySingleton.getInstance(mActivity).getImageCache().evictAll()

相關問題