How to remove a particular image URL from disk cache in Glide? - android

I am loading an image from remote URL into an ImageView with Glide. When I update the image, the content within that URL changes, but the URL location remains same. (This is as expected).
for eg. https://www.the-android-app.com/userid/121.jpg
Now I am showing image into ImageView using this code.
Glide
.with(this)
.load(profilePicUrl) //this is in String format
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.skipMemoryCache(true)
.error(R.drawable.default_profile_pic)
.into(imgProfilePic);
When I update my profile pic, Glide is loading the same old pic from Cache. I need to remove cache entry for this URL before fetching the new image.
Edit:
The url always remains same, only the content inside it changes.
I'm using Glide 3.8.0

You can try signature(new ObjectKey(System.currentTimeMillis()))
Look in Custom cache invalidation for more information.
Example
GlideApp.with(this)
.load(imageUrl)
.signature(new ObjectKey(System.currentTimeMillis()))
.into(imageView);

Related

Efficient and faster way of loading images from url

I am developing A App which requires loading multiple images from their urls. I save all the images in firebase storage. I am currently using a compressor to reduce the size.
newImageFile=new File(uri1.getPath());
try {
compressedImageFile = new Compressor(ProductAdd.this)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumbData1 = baos.toByteArray();
and I using Glide to load the image from the url
Glide.with(context).load(imgUrl).into(holder.cutImg);
I wanted to know if there is any way I could improve to load images much faster. Maybe store them in a specific format.
You can load a thumbnail of the original image into the ImageView before the original image loads.
Glide.with(fragment)
.load(url)
.thumbnail(0.05f)
.into(imageView);
This will load the 5% quality of the original image into the view and when the complete image loads it will be replaced.
And if you're having a separate URL for thumbnail then
Glide.with(fragment)
.load(url)
.thumbnail(
Glide.with(fragment)
.load(thumbnailUrl))
.into(imageView);
Now when image loads it will be immediately loaded into the view, here you can use the transition for a smooth effect.
Glide.with(fragment)
.load(url)
.thumbnail(0.05f)
.transition(DrawableTransitionOptions.withCrossFade())
.into(view);
You can checkout my project in which I'm also first compressing the Image and storing a thumnail image to Firebase Storage and then loading it.
You can also try different DiskCacheStrategies but I would suggest to go with the default one, it works just fine!
Here are various DiskCacheStrategies:
.diskCacheStrategy(DiskCacheStrategy.ALL)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
You can use the disc strategy which will be used to store the image on the disc. So it may take time for loading at first but later it does not take time at all.
For Glide 4.x
Glide
.with(context)
.load(imgUrl)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(holder.cutImg);
For Glide 3.x
Glide
.with(context)
.load(imgUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.cutImg);
Please note: This will not refresh the image if you just update the image without updating URL. SO if you want to see the updated image you must update URL and Glide will automatically download an image for you.
if you are using firebase storage for host your images : Using FirebaseUI you can quickly and easily download, cache, and display images from Storage with Glide. source
Glide.with(context).load(model.imageUrl).thumbnail(0.05f).into(holder.imageView)

Image not updating which is uploaded using volley and glide

I am using Volley library in my app to upload image and Glide for setting image to textview. I have button to update image which opens gallery and let user select the image. The file is then uploaded in the specific folder successfully, but the image remains same. I also tried logout and login user which erases the values in ShredPreferences, all data updates, but image remains same. The older image is replaced by new in server folder, but still shows in app. It only updates after clearing app data from Settings. Why is this happening? does glide stores the cache data? how to get rid of this?
Glide caches the image from server by default. So if your URL is not changing, it will show you the image from the cache. To work around this, you can use RequestOptions:
RequestOptions requestOptions = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
Glide.with(this)
.load(YOUR_IMAGE_URL)
.apply(requestOptions)
.into(YOUR_IMAGEVIEW);
EDIT (Picasso)
If you are using Picasso, the same case applies. In this case you should use:
Picasso.with(this)
.load(YOUR_IMG_URL)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(YOUR_IMAGEVIEW);

cache image without showing glide

Is it possible to cache image using glide without showing it in the imageView?. If it is then how?.
Right now I'm doing this code:
Glide
.with(getApplicationContext())
.load("imageUrl")
.override(windowWidth(),(int)windowWidth()*0.5))
.diskCacheStrategy(DiskCacheStrategy.ALL);
But this is not working , when app is open glide load image not from cache but from url.
Since glide 4.X:
//to save img
RequestManager rm = Glide.with(context);
rm.load(imgUrl).submit();
//to load img
Glide.with(context)
.applyDefaultRequestOptions(
new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
.load(imgUrl)
.into(view);
Based on this GitHub topic
i've never used it, but referring the documentation: have you tried the downloadOnly?
Glide's downloadOnly() API allows you to download the bytes of an image into the disk cache so that it will be available to be retrieved later.
https://github.com/bumptech/glide/wiki/Loading-and-Caching-on-Background-Threads#downloadonly
To preload remote images and ensure that the image is only downloaded once:
Glide.with(context)
.load(yourUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.preload();

how to cache image with a new one which have the same url?

i have listview to load images for alot of users and this listview item has an imageview for profile picture of user, the problem here if the user changed his the new image will not be shown as last profile picture is cached and the old one will be displayed because the new and old image have the same url:
URL EX:
"https://xyz.s3.amazonaws.com/users/" + friend_id + "/photos/profile.jpg"
i used Glide but i have the same problem
This issue is because of the cache of images, Glide checks if image is available in cache, if it does glide loads it else it would load the new image.
To solve this issue you need to change its cache strategy like this
Glide.with(mContext)
.load((Integer) mDataset.get(position))
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE) // this will prevent image to be cached and each time glide will load it from server
.into(imageView);

Captcha image cannot be refresh by using Glide

I have tried to use Glide to load an Captcha image into a ImageView. The first time loading is fine. However, when I reload the Captcha image into the same ImageView, the ImageView does not refresh to new image. Does anyone have idea how to solve this issue?
String url = "https://captcha_path";
ImageView imgView = (ImageView)getActivity().findViewById(R.id.imgView);
Glide.with(getActivity()).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).into(imgView);
You can always use Glide.clear() and then call Glide.with(...).load() again. If your url doesn't change when the image changes, you may also need to add .skipMemoryCache(true) to your load call. For more control, check out the .signature() API. You can always do something like:
Glide.with(fragment)
.load(url)
.signature(new StringSignature(UUID.randomUUID().toString()))
.into(imgView);
Glide.with(fragment)
.load(url)
.signature(new StringSignature(UUID.randomUUID().toString()))
.into(imgView);
Replace StringSignature with ObjectKey (for Glide v4)
Glide.with(fragment)
.load(url)
.signature(new ObjectKey(UUID.randomUUID().toString()))
.into(imgView);

Categories

Resources