有没有办法强迫Volley刷新图像?

时间:2014-05-05 12:08:56

标签: android android-volley

我目前正在开发一款前端应用,它非常依赖从网络服务下载的图像。为了简短起见,前端从我们的webservice下载一个具有3个图像插槽的给定用户配置文件。如果这样的用户上传了一张新图片(保持相同的网址),它就不会刷新,直到我杀了我的应用程序,所以如果我把它放在后台并恢复数据就不会被更改。

以下是处理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;
}

缓存类:

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);
}

提前谢谢大家!

编辑:我一直在努力寻找解决方法,到目前为止我想出了这个:

如果我在调用getBitmap(url)时将新图像放在分配给图像网址的缓存中我会收到新图像,但问题仍然存在,如果我用背面关闭应用程序按钮并再次打开它可以加载旧图像,但如果我杀了应用程序,它将加载新的...这里是我用来将图像放在缓存上的代码:

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

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

1 个答案:

答案 0 :(得分: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()