Android Glide failing to load image - android

I am trying to load an image into an ImageView using the Glide library. The URL that I pass into the load() method points to an endpoint ('/api/user/5/logo'), which then returns the image. For some reason this doesn't seem to work.
However if I pass a URL that points directly to an image ('www.example.com/logo.png'), it works perfectly.
How do I get Glide to work perfectly with the required web service?
To recap, this works:
Glide.with(getContext())
.load("www.example.com/content/logo.png")
.centerCrop()
.placeholder(R.drawable.mark)
.into(imageView);
.. and this doesn't:
Glide.with(getContext())
.load("www.example.com/api/user/5/logo")
.centerCrop()
.placeholder(R.drawable.mark)
.into(imageView);
Any help would be greatly appreciated.

Try with this:
Glide.with(getContext())
.load("http://www.example.com/api/user/5/logo").centerCrop().into(imageview)

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

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.

Glide load thumbnail not working

I'm using Glide to load thumbnail from videos but it doesn't seem to be working on my app. The ImageView is just empty for some reason.
Glide.with(context)
.load(url)
.asBitmap()
.thumbnail(0.1f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(target);
I added a listener, so I can figure out what is wrong but the Exception thrown is null.
I tried using ThumbnailUtils using the same url because I thought there might be something wrong with it but the thumbnail loads fine.
Anyone experiencing the same? I'm using a Nexus 7 (6.0.1)
Even i stumbled upon the same situation. Somehow from the uri its not loading the image unless create a file instance using local file path. So to make it work i used it like below
Glide.with(mContext).load(Uri.fromFile(new File(path)).into(icon);
In the doc they are using the same approach. You can refer here : Glide - Videos
Apart from that i also noticed unusual behaviour of using the cache. If you are using cache strategy as DiskCacheStrategy.ALL or DiskCacheStrategy.SOURCE it doesn't load the thumbnail but if i am using DiskCacheStrategy.RESULT it works. Hope it helps
As explained in Glide documentation, this feature is only available for videos stored locally on the device.
Also, you should use a path like /storage/emulated/0/Pictures/example_video.mp4. Adding file:/// before this path won't work out either.
You can find more informations here : https://futurestud.io/blog/glide-displaying-gifs-and-videos
Cheers !
You can use override, which surely works:
Glide.with(context)
.load(url)
.crossFade()
.override(width, height)
.into(imageView);
Glide.with(mcontext)
.applyDefaultRequestOptions(RequestOptions.centerCropTransform()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE))
.load(videourl)
.into(thumbnailimg);
Try using this code
#BindingAdapter("videoThumbnailFromUrl")
fun setImageFromVideoUrl(imageView: ImageView, url: String) {
val thumb = 10000L
val options = RequestOptions().frame(thumb)
Glide.with(imageView.context).load(url).apply(options).placeholder(R.drawable.place_holder).into(imageView)
}

Picasso image loading previously cached images

I am using picasso library in my project to fetch url images from the server into my ImageView, but unfortunately picasso loading the previously cached images from disc instead of loading the new images from url. Please find the below piece of code for your reference.
Picasso.with (ImageActivity.this)
.load(url)
.placeholder (R.drawable.default_user)
.error (R.drawable.default_user)
.into(imgUser);
I have also tried the solutions like:
Picasso.with(ImageActivity.this).invalidate(url);
and
Picasso.with (ImageActivity.this)
.load(url).memoryPolicy(MemoryPolicy.NO_CACHE)
.placeholder (R.drawable.default_user)
.error (R.drawable.default_user)
.into(imgUser);
But nothing is working for me. Kindly help me to resolve my issues.
Thanks in advance.
Try :
Picasso.with(mContext)
.load(URL)
.memoryPolicy(MemoryPolicy.NO_CACHE )
.networkPolicy(NetworkPolicy.NO_CACHE)
.error(R.drawable.xxx)
.into(xx);
I know there's already an answer but after trying a few ways I figured out a very simple way:
Check if the image is null - if it is not - then load the image with Picasso-like that:
if(vImage.getDrawable() == null)
Picasso.with(context).load(URL).into(vImage);
Hope this helped someone.

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