I'm using the Picasso library to dynamically load images from an API call.
Certain urls seem to return a malformed URL, for example: "http://imagesite.com/image.1241123.gif which crashes the app.
Picasso.with(activity).load(image)
.centerCrop()
.error(R.drawable.icon01)
.resize(50, 50)
.into(icon);
Looking at the documentation, I assumed the .error() parameter would handle this, but Picasso seems to see it as a valid URL, even though it won't return an image. I've also tried using the Picasso.Builder but I continue to get the same errors. Any suggestions?
picasso don't load gif image for that you have to use Glide library.
Check this link it might help you : http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
and you get library from : https://github.com/bumptech/glide
error() will handle issues where the URL is well formed but for some reasons it cannot display the image.
If you have malformed url it means the problem is your data. Remember garbage in , garbage out.
Use Uri.parse() before loading it to picasso, then handle the thrown exception: better fix your data.
Related
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);
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)
}
I am using Picasso to handle image loading and caching in my Android Udacity project and I am noticing the caching is not working as I'd expect:
As you can see on the left fragment, the image has already loaded in an earlier thread. Now, with the same URL link, I am asking Picasso to place that image in the fragment on the right.
Here is the code which generates the grid view on the left fragment (and occurs first):
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/adapters/GridViewAdapter.java#L71
String load = String.format(sImgUrl, sImgSize, movie.poster_path);
Picasso.with(mContext.getApplicationContext())
.load(load)
.placeholder(R.drawable.abc_btn_rating_star_on_mtrl_alpha)
.error(R.drawable.abc_btn_rating_star_off_mtrl_alpha)
.resize(550, 775)
.into(viewHolder.imageView);
And then here is the code which runs in the right fragment:
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/fragments/MovieDetailsFragment.java#L308
Picasso.with(getActivity().getApplicationContext())
.load(String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
.error(R.drawable.blank)
.fit()// .resize(366, 516)
.into(mMovieThumb, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
#Override
public void onError() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
});
I am using the same application context in each as well as the load text:
String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
and
getActivity().getApplicationContext()
So, I would think Picasso ought to detect when the exact same URL load link appears in the same context within a short period of time from each other and Picasso would then load the exact same image back into the app.
If this is not how Picasso caching works, then how does it?
As a comment mentioned, I'd guess this is affected by the size of the image being different in both fragments.
I'd recommend using https://github.com/facebook/fresco instead of picasso. It's more efficient, especially with different sizes. You can also directly access cached files if required https://github.com/facebook/fresco/issues/80
It's probably related to the HTTP headers received when getting the image that do not allow caching, as Picasso relies on an HTTP component to do the caching.
Try uploading your image on imgur, try hardcoding that path and see if it works. If that's the case, you'll have to find a workaround on how to get the image from the movie database.
So, I was starting my project and wants to use Picasso in my project because its popular and used by many projects out there.
I included picasso using gradle and tried loading facebook profile url with this. http://graph.facebook.com/rohitiskul/picture.
It worked very well. It loaded image from network without any issues. I restarted the app.(Without actually killing the process). It showed me the same image instantly cached in Memory.
But then, I killed the app (force stop) and restarted. It took almost 10+ seconds to load the image. And that image was loading from the disk when I checked in the debug logs.
My code looks like this -
In MainActivity-
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.into(imageView);
In application class-
Picasso picasso = new Picasso.Builder(this)
.indicatorsEnabled(true).loggingEnabled(true).build()
Picasso.setSingletonInstance(picasso);
Anyone with the similar problem? Any solution would be helpful.
I tried loading same Url with UniversalImageLoader and it was fast when fetching cached image from disk.
Edit
Earlier while playing with my app, I found out that Picasso wasn't loading the disk cached image when device was offline.
I encounter the same problem,
but find only slow for the first image, later images will be fast.
Probably it needs a warm-up (loading index cache) ?
Okay i got your problem. I have fixed it by doing this
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
#Override
public void onSuccess() { }
#Override
public void onError() {
// Try again online if cache failed
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.into(imageView);
}
});
Explanation:
Picasso will look for images in cache.
If it failed only then image will be downloaded over network. In your case from facebook.
This issue I had also faced earlier, with this what I understood that Picasso refer the cached image based on image name mentioned in the URL.
In your case you don't have image name in URL like 'image1.jpg'. Due to which Picasso is finding it difficult to read from cache and it downloads the image everytime
You can give a try to image containing the image name in URL and that will work
Picasso doesn't offer disk cache out of the box. Instead, it relies on an Http Cache.
Make sure you add OkHttp to your dependency list.
Add a string identifier with the stableKey method when making the request so Picasso can identify your requests and quickly load it from the cache.
Example:
Picasso.Builder(context).loggingEnabled(true).build()
.load(imageUrl)
.stableKey("myImage")
.into(imageView)
im using picasso library to load images.
However in my application, users can have profile pictures and the link for the image is constant... Picasso has no clue that the image has changed...
I tried using : .skipMemoryCache() but it wasn't an ideal solution...
Is there a way to check if there's a new picture in the same link using picasso? Thanks!
Apparently, there is no API in the current (2.3.2) version of Picasso to achieve this (but it is a work in progress - see this bug).
That aside, if you have control of the server side, you may want to think about your design decision to provide the changing profile picture at a constant URL.
An alternative would be: Include the current profile picture URL in the profile information you retrieve. This way, your cache can use the cached image - and as soon as the profile information provides a new URL, Picasso will fetch it. In all other cases, Picasso can leverage the cache.
I got the answer from this link
change your URL as shown below:
String imageurl = url + "?time=" + System.currentTimeMillis();
Picasso.with(getContext()).load(imageurl).into(imageView);
this worked for me. thanks
Picasso.with(context)
.load(url)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.fit()
.placeholder(YOUR PLACE HOLDER RESOURCE)
.centerCrop()
.into(imageView);
One solution is to invalidate the cache like so
Picasso.with(context).invalidate(imagePath);
The other way to force download the image which then is cached for subsequent use as mentioned in the code.
Picasso.with(context)
.load(imagePath)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(userAvatar);