Volley NetworkImageView clear cached image
Asked Answered
A

6

8

Does anybody know if it is possible to clear the cached image from a single NetworkImageView using Googles Volley library?

I have an avatar image in a NetworkImageView and would like to show the new image once I have uploaded it to the server. At the moment if I do profileImg.setImageUrl("myUrl", mImageLoader); I get the cached image.

Abdominal answered 13/8, 2014 at 13:10 Comment(1)
Check this : #17230931Bachelor
A
3

So to belatedly answer my own question. I ended up taking a different approach to ensure that I always get the latest copy of an avatar image and it turned out to be very simple.

Instead of trying to clear out an individual image from the cache I use the following method.

int time = (int) (System.currentTimeMillis());//gets the current time in milliseconds
String myUrl = "www.mySite.com?timestamp=" + String.ValueOf(time);
profileImg.setImageUrl("myUrl", mImageLoader);

What this does is to append an imaginary parameter to the url I call to get the image with a current timestamp. This guarantees that I am always making a call to a unique url and so therefore am always getting the most up-to-date version of that image.

The beauty of this approach is that it is not limited to Volley and can be used however you choose to make network calls.

Abdominal answered 28/1, 2015 at 21:57 Comment(3)
That is hardly a solution. It means you are redownloading the image every time you want to display it, even if it hadn't changed since the last time you downloaded it.Anteater
It's a solution that works given the situation. Care to offer an alternative?Abdominal
Sure. Just expose a method to remove keys from your image cache, with a twist: 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.Anteater
B
5

Check this out :

1) Turning off cache : If you want disable the cache for a particular url, you can use setShouldCache() method as below.

StringRequest stringReq = new StringRequest(....);
stringReq.setShouldCache(false);

2) Deleting cache for particular URL : Use remove() to delete cache of an URL.

yourRequestQueue.getCache().remove(url);

3) Deleting all the cache :

yourRequestQueue.getCache().clear(url);

Also, check out this link here.

Hope this helps.

Bachelor answered 14/8, 2014 at 7:5 Comment(3)
none of the above approach is working for network imageview. any idea?Untrue
Same case with me, nothing works, anybody found solution?Dewclaw
check this link : #25333851. Check the answer from "Versa"Bachelor
A
3

So to belatedly answer my own question. I ended up taking a different approach to ensure that I always get the latest copy of an avatar image and it turned out to be very simple.

Instead of trying to clear out an individual image from the cache I use the following method.

int time = (int) (System.currentTimeMillis());//gets the current time in milliseconds
String myUrl = "www.mySite.com?timestamp=" + String.ValueOf(time);
profileImg.setImageUrl("myUrl", mImageLoader);

What this does is to append an imaginary parameter to the url I call to get the image with a current timestamp. This guarantees that I am always making a call to a unique url and so therefore am always getting the most up-to-date version of that image.

The beauty of this approach is that it is not limited to Volley and can be used however you choose to make network calls.

Abdominal answered 28/1, 2015 at 21:57 Comment(3)
That is hardly a solution. It means you are redownloading the image every time you want to display it, even if it hadn't changed since the last time you downloaded it.Anteater
It's a solution that works given the situation. Care to offer an alternative?Abdominal
Sure. Just expose a method to remove keys from your image cache, with a twist: 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.Anteater
M
1

in the LruBitmapCache you should do diable put(url, bitmap) :

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

by this way every time you call the volley method image don't save to catch

Maestas answered 28/11, 2017 at 13:23 Comment(0)
T
0
  To turn off the cache:

1
request.setShouldCache(false);
to remove the cache for a specific request:

1
queue.getCache().remove(url);
to clear all cache:

1
queue.getCache().clear();
to invalidate the cache: this will allow to display the cached data until the response is received. When the response is received, it will automatically override the cached data.

1
queue.getCache().invalidate(url, true);

    for more details you can refer to the 
    url:http://androidresearch.wordpress.com/2014/02/01/android-volley-tutorial/
Tillis answered 13/8, 2014 at 13:29 Comment(0)
C
0

If you create the cache like this:

RequestQueue requests = Volley.newRequestQueue( context );
BitmapCache cache = new BitmapCache(cacheSize);
ImageLoader loader = new ImageLoader( requests, cache );

You can clear all like this:

public void clear () {
    cache.evictAll();
    requests.getCache().clear();
}
Caston answered 10/3, 2015 at 16:25 Comment(0)
A
0

I have created volleySingleton Class. I load image from ImageLoader . I have a requirement of deleting the cache but not able do that. Used all these three method

1) Deleting cache for particular URL : Use remove() to delete cache of an URL.

VolleySingleton.getInstance().getRequestQueue().getCache().remove(ImageUrl); 2) Deleting all the cache :

VolleySingleton.getInstance().getRequestQueue().getCache().clear();

3) VolleySingleton.getInstance().getRequestQueue().getCache().invalidate(ImageUrl,true);

I have tried all of them but cashe is not getting deleted

Anaplastic answered 3/3, 2017 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.