1

I'm using Volleys library for image downloading. When the user changes the profile image, I upload the image to the server to replace the previous image, since I'm using the user's ID in the database the link to the profile image is always the same.

The issue I have with the cache is that it can't replace the bitmap inside the cache, after the server returns a successful response with a link I want to replace the image in the NetworkImageView, but that doesn't seem to work. Here is my LruImageCache class

public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(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);
}

public void clear(){
    this.clear();
}

public void removeBitmap(String key){
    this.remove(key);
}

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

}

I have the class in my AppController like this

public class AppController extends Application {

public static final String TAG = AppController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private LruImageCache lruBitmapCache;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
    lruBitmapCache = new LruImageCache();
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue, lruBitmapCache);
    }
    return this.mImageLoader;
}

public LruImageCache getLruBitmapCache() {
    return lruBitmapCache;
}

}

After fully uploading the image and getting the url returned from the server, I have tried to just use this line but it didn't work

 Image.setImageUrl(new_url, AppController.getInstance().getImageLoader());

I also tried to call the LruCache to remove the bitmap from the cache so that I would set the image back again, but doesn't seem to work either.

 AppController.getInstance().getLruBitmapCache().remove(new_url);

I have also tried to download the bitmap manually using a different method then putting in in the cache using the new_url as the key but still doesn't work.

  AppController.getInstance().getLruBitmapCache().put(new_url, newBitmap) 

Any help, How do I replace the bitmap in the cache so that It would load the new uploaded imagefile? I'm sure the image file has been uploaded to the server because I can open it and see its replaced. The only way it works right now is if I close the app and re-open it again.

user3564573
  • 680
  • 1
  • 12
  • 24
  • Volley stores keys as a combination of the width and height of the image, with the URL. So instead of removing the key string, you will have to remove any value whose key contains the URL. – Carlos Jan 14 '16 at 11:07

0 Answers0