Android Glide download before display - android

I'm using Glide and I want to know if it's possible to download all images without display them to fill the cache to display them later ? I tried this but the images are not downloaded :
for (String url: urls) {
Glide.with(getBaseContext()).load(url);
Log.v("Download", "processing");
}
Can you help me ?

I solved my problem we can use this to download the images :
Glide
.with( context )
.load( "http://futurestud.io/icon.png" )
.downloadOnly(2000, 2000);
And to get and display the images in the cache, we should use the same url and add the diskCacheStrategy parameter like this :
Glide.with(context)
.load( "http://futurestud.io/icon.png" )
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);

You can use asBitmap method in Glide to do the same.
Glide.with(this).
load(url).
asBitmap().
into(100, 100). // Width and height
get();

Glide version 4.6.1
RequestOptions requestOptions = new RequestOptions().priority(Priority.IMMEDIATE);
Glide.with(appContext)
.download(model)
.apply(requestOptions)
.submit();

Related

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.

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 create center cropped thumbnail images from image url in android

I am using glide library for loading my images , i am showing entire images , i want to show thumbnail image. How to do it?
Glide.with(context)
.load(resized)
.centerCrop()
.placeholder(R.drawable.default_review)
.crossFade().fitCenter()
.into(triviaImage);
I have tried scaleType:fitXY and scaleType:centerCrop nothing worked.
please see image
You should have to use override(width, height) as shown here:
Glide.with(context)
.load(resized)
.asBitmap()
.override(400, 400) // smaller the size, lower the quality of Image
.centerCrop()
.error(R.drawable.image_error_placeholder)
.into(triviaImage);
try this,
Glide
.with( context )
.load( resized )
.thumbnail( 0.1f )
.into( triviaImage);
I fix my problem by this -
Glide.with(context)
.load(myImageUrl)
/*Here "myImageUrl" is the original image url*/
.thumbnail(Glide.with(context).load(myThumbImageUrl).centerCrop())
/*Here "myThumbImageUrl" is the thumb image url*/
.into(view);

Using Glide for Android, how do I load images from asset and resources?

I want to keep all the transformation, stoke and animations identical and was thinking if we can pass resource ID or asset name in Glide to load it locally?
For resource ids, you can use:
Glide.with(fragment)
.load(R.drawable.resource_id)
.into(imageView);
For assets, you can construct an asset uri:
Glide.with(fragment)
.load(Uri.parse("file:///android_asset/<assetName>"))
.into(imageView);
Glide
.with(context)
.load(uri)
.asBitmap()
.placeholder(R.drawable.yourimage)
.error(R.drawable.yourimage)
.into(yourview);
Apart from the above answer if the image URL return null you can load default image into the view as like above.
It works when using .asBitmap()
String pathUri="file:///android_asset/img/flower.jpg";
Glide.with(context).asBitmap().load(Uri.parse(pathUri)).into(holder.imgView_post);

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