0

i have Volley network ImageView , problem is when image is changed inside sever volley still uses cached image which is old one , is there anyway that volley understand that image changed inside server ? or if there is't , how can i prevent it from caching ?

here is my current code :

private void networkImage(){
    String url = "http://example.com/profilePictures/"+username+".jpg";

    networkImageView = (NetworkImageView) findViewById(R.id.IBProfilePicture);
    networkImageView.setOnClickListener(this);

    RequestQueue networkQueue = Volley.newRequestQueue(getApplicationContext());
    networkQueue.getCache().remove(url);
    networkQueue.getCache().clear();
    ImageLoader networkImageLoader = new ImageLoader(networkQueue, new ImageLoader.ImageCache() {
        @Override
        public Bitmap getBitmap(String url) {
            return null;
        }

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

        }
    });

    networkImageView.setImageUrl(url,networkImageLoader);
}
Sajed Ng
  • 107
  • 1
  • 7
  • recommand for add extra timestamp argument for user profile image url. Maybe you could check this one: http://stackoverflow.com/a/25302006/188784 – Gohan Sep 12 '16 at 08:24

2 Answers2

3

Try this solution:

private void networkImage(){
    String url = "http://example.com/profilePictures/"+username+".jpg";

    networkImageView = (NetworkImageView) findViewById(R.id.IBProfilePicture);
    networkImageView.setOnClickListener(this);

    RequestQueue networkQueue = Volley.newRequestQueue(getApplicationContext());
    ImageLoader networkImageLoader = new ImageLoader(networkQueue, new ImageLoader.ImageCache() {
        @Override
        public Bitmap getBitmap(String url) {
            return null;
        }

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

        }
    });

    networkImageView.setImageUrl(url+"?time="+System.currentTimeMillis(),networkImageLoader);
}
mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • It works perfectly , but all of those cached images are going to fill the storage , aren't they ? – Sajed Ng Sep 12 '16 at 08:32
  • Yes, It make other store cache for your image. In your case only have profile picture updated, I think have no problem with storage. – mdtuyen Sep 12 '16 at 08:48
  • First of all Thank you , is there any limit for cache ? I mean app will contain more than hundred users which can view each other's profile , if there is a limit for cache and it won't fill entire storage , I'm OK with that . – Sajed Ng Sep 12 '16 at 09:11
3
  1. changes the server's cache strategy if you can.
  2. you can use request.setShouldCache(false) to disable cache for a request.

for method 2 you can do that by overriding ImageLoader.makeImageRequest();

public class NoCacheImageLoader extends ImageLoader {
    public NoCacheImageLoader(RequestQueue queue, ImageCache imageCache) {
        super(queue, imageCache);
    }

    @Override
    protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight, ImageView.ScaleType scaleType, String cacheKey) {
        Request<Bitmap> request =  super.makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType, cacheKey);
        // key method
        request.setShouldCache(false);
        return request;
    }
}

then use NoCacheImageLoader instead of ImageLoader. Now it will not cache the image into disk.

by the way you should still add timestamp to the url, because if the urls are the same, the NetworkImageView will not load the image again.

AssIstne
  • 466
  • 4
  • 13