cache image without showing glide - android

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

Related

Glide Does not use Cached Image

Hi I am using Glide to load image and after following many posts I tried to implement Glide caching using
RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
Glide.with(mContext)
.load(imageUrl)
.transition(ImageUtil.crossFadeTransition())
.apply(ImageUtil.requestOptionsForSlider())
.listener(glideRequestListner)
.into(imageView)
My knowledge is that if I use diskCacheStrategy(DiskCacheStrategy.ALL) then the above Glide image loading code will automatically use cache if available, or download if not, right?
But I see everytime the code is called, (checking my internet speed meter) that it is always fetching/downloading from the given url (even though I can see the app memory size is increasing).
I even tried this code also-
val future: FutureTarget<File> = Glide.with(mContext)
.load(imageUrl)
.downloadOnly(500, 500)
Nothing seems to work
What should I do so that Glide uses cache if available?What is wrong with my conception about the caching implementation? Any help is very much appreciated!
Are you sure it is downloading again ? did you try disabling internet just before loading the images ?
Otherwise, is your image URL always the same for the same image ? (as Glide uses it as a cache key).
You can use this in the following calls to check if images are in cache:
Glide.with(fragment)
.load(url)
.onlyRetrieveFromCache(true)
.into(imageView);

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

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

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

How to reload image in Glide from the same url?

I am using Glide image loader to load an image from a specific URL, now if I update the image to the same URL, Glide is still showing the cached image in my imageview. How to reload the image from the same URL?
As per the Glide wiki Caching-and-Cache-Invalidation
Option 1 (Glide v4): Use ObjectKey to change the file's date modified time.
Glide.with(requireContext())
.load(myUrl)
.signature(ObjectKey(System.currentTimeMillis().toString()))
.into(myImageView)
Option 1 (Glide v3): Use StringSignature to change the file's date modified time.
URLs - Although the best way to invalidate URLs is to make sure the server changes the URL and updates the client when the content at the URL changes, you can also use StringSignature to mix in arbitrary metadata
Glide.with(yourFragment)
.load(url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis()))
.into(yourImageView);
If all else fails and you can neither change your identifier nor keep track of any reasonable version metadata,
Option 2: You can also disable disk caching entirely using diskCacheStrategy() and DiskCacheStrategy.NONE
Glide.with(Activity.this).load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE )
.skipMemoryCache(true)
.into(imageView);
Reference: https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Use .diskCacheStrategy(DiskCacheStrategy.NONE)
Glide.with(context)
.load(url)
.apply(new RequestOptions()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true))
.into(ImageView);
Add
signature(new ObjectKey(String.valueOf(System.currentTimeMillis())))
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.cover_placeholder);
requestOptions.error(R.drawable.no_image_available);
requestOptions.signature(
new ObjectKey(String.valueOf(System.currentTimeMillis())));
Glide.with(MosaicFragment.this)
.setDefaultRequestOptions(requestOptions)
.load(finalPathOrUrl)
Use below code, it working fine for me. Set diskCacheStrategy(DiskCacheStrategy.NONE) and skipMemoryCache(true). It will load the image every time.
Glide.with(Activity.this)
.load(theImagePath)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(myImageViewPhoto);
After struggling for a couple of hours, I find the solution.You can mix in the datetime of file by adding StringSignature.So when the file will load,It will always use the latest one.Like this
Glide.with(this)
.load(image_url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(imageview);
Reference : https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
I used a work around since no other method worked for me
In my case I knew that when the image on the server will change so I was just adding ?= at the end of the url, for glide it will be a new url and instead of using cache Glide will load the new image.

Categories

Resources