what if glide gives an error in andorid - android

I am stuck thinking that I am developing an which provide a way to advertise their products, but I am confused what if glide gives an error in certain position of recyclerview, what should I do and how to manage that? Should I remove recyclerview item at that position or do something else because it degrades my app quality? One more thing, why the popular apps ex: facebook, instagram does not get such errors, because I never found any error image in their recyclerview how they do it?
my backend is firebase;

If Glide cannot load image at specific place. You can use a placeholder or error to managed it.
See docs:
https://bumptech.github.io/glide/doc/placeholders.html
My Facebook app sometime cannot load image because my internet connection too slow, and it become blank.

Facebook and other application has button to retry, when error comes. you can do as below in bindViewHolder method for RecyclerView and getView method for ListView:
Glide.with(mContext)
.load(arrayList.get(position).getImageUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<super GlideDrawable> glideAnimation) {
if (holder.mimageView.getVisibility() == View.GONE)
holder.mimageView.setVisibility(View.VISIBLE);
if (holder.btnRetry.getVisibility() == View.VISIBLE)
holder.btnRetry.setVisibility(View.GONE);
holder.mimageView.setImageDrawable(resource);
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
if (holder.mimageView.getVisibility() == View.VISIBLE)
holder.mimageView.setVisibility(View.GONE);
if (holder.btnRetry.getVisibility() == View.GONE)
holder.btnRetry.setVisibility(View.VISIBLE);
}
});
Now on btnRetry click you can again fetch image using glide as above.

Related

Image are not displaying from Cache memory using Picasso Library

I am using Picasso library for image download and displaying it in imageView. This library also store images in cache and memory. When my internet is turn on, i am able to view images on imageView. So i think, it should also be store in cache or file memory. Now my internet is turnOFF, but it doest not display to images. kindly have a look.
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError(){
holder.loadingBar.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
});
}
});
Finally I resolved that issue. Thanks #dev.bmax
image url was not correct. There is bug in Picasso. If we have url like as
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg?500|700
Picasso is able to displaying image when internet TURN ON
but if we TURN OFF to internet, it does not decode the url. and not display the image also.
We have to remove ?500|700 then i was able to view image in OFFLine mode also.
//url.substring(0,url.indexOf("?"))
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg
Thanks!

Glide wont load all images in ViewHolder

So I have a RecyclerView with a custom adapter and I use Glide to download images from a URL when applicable. Relevant code snippet here:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Location location = mLocations.get(position);
holder.title.setText(location.getTitle());
if (location.hasImage()) {
String imageUrl = NetworkHelper.getImageUrl(getContext(),location.getImageName());
holder.image.setVisibility(View.VISIBLE);
// Use Glide for image loading
Glide.with(getContext())
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.into(new GlideDrawableImageViewTarget(holder.image) {
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
e.printStackTrace();
}
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
}
});
} else {
// Due to the nature of the RecyclerView, we need to ensure we clear
// out images to odd behavior.
Glide.clear(holder.image);
holder.image.setVisibility(View.GONE);
}
}
Problem is that the onResourceReady() method isnt always being called and hence not all images being loaded. Ive verified all the URL's being passed and they all seem fine. Is there a Glide specific issue here perhaps? If I take a URL that is not being loaded and hard code it into the imageUrl variable, its still not being downloaded, so there must be something specific to those images thats causing glide to fail. A timeout issue perhaps?
EDIT: I added the onLoadFailed method and it does hit, but the Exception is null, which is kinda weird. Basically same issue as described here: https://github.com/bumptech/glide/issues/741

Android: Glide onStart/onResume bug

I have a weird bug with Glide and i'm not sure what to do about it. Basically I have this function, which loads images from reddit.com/r/earthporn/
newsdata.data.children[position].data.url is a list of urls
public void displayImage(final int position) {
Drawable d = view.getContext().getResources().getDrawable(R.drawable.ic_collections_white_24dp);
System.out.println(newsData.data.children[position].data.url);
Glide.with(view.getContext())
.load(newsData.data.children[position].data.url)
.asBitmap()
.listener(new RequestListener<String, Bitmap>() {
#Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
displayImage(position + 1);
//todo: make sure we don't run into a index out of bounds.
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.centerCrop()
.error(d)
.into(new BitmapImageViewTarget(view.getImageView()) {
#Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
view.setImageBitmap(resource);
wallPaperBitmap = resource;
System.out.println(resource);
}
});
}
Android can't load every single image that appears on the subreddit. When that happens, I load the next image down. On a run today, it will load this image http://imgur.com/HOSg2FC, call an exception, and then load this http://i.imgur.com/baluFH7.jpg
OnCreate, this code will run as expected. It runs, hits the exception, calls displayImages() again and sets the correct Image. (Please, let me know if there's a better way to do this)
OnResume/onStart is where the problem lies. The code will run, hit the exception, place the error drawable, call displayImages again but not change the imageview. setResource(bitmap resource) will also give me the correct bitmap but will not let me update the imageview at all.
Basically, it freezes the imageview to the error drawable, or to a blank white screen if there is no drawable and won't let me change it.
Calling onResume with the following code:
if(wallPaperBitmap != null) {
view.setImageViewPicture(wallPaperBitmap);
//view.showButtons();
System.out.println("bitmap != null");
}
else {
retrieveImage(SubReddit.Earth);
System.out.println("bitmap = null");
}
Will set the correct picture.
I thought maybe this might be a problem with the cache so I tried using diskcachestrategy.none but that didn't help either. As a last resort, I could probably refresh the screen once setResource is called with the updated bitmap or just use Picasso but I'd rather avoid that.
Any help is appreciated!
Edit: I'm also getting the error: D/skia﹕ --- SkImageDecoder::Factory returned null onStart
Glide may restart failed requests in onStart, which would trigger the failure, then set the error Drawable. To fix this, you should do two things:
Always call clear() on Views and Targets before calling setImageResource/setImageDrawable yourself (outside of onResourceReady). Calling clear() will prevent Glide from managing that View/Target, which in turn will stop Glide from restarting the loading in onStart.
If you ever keep a reference to a resource loaded by Glide (usually a Bitmap or a Drawable, you need to make sure to null out your reference when the Target's onLoadCleared or onLoadFailed methods are called. This isn't directly related to this issue, but it's an error in your code above and can lead to weird errors if Glide re-uses the Bitmap while you're referencing it.

Does Picasso library for Android handle image loading while network connectivity is off?

I'm working on an application in which i use Picasso library for image loading in my ViewPager and other ImageViews. So i want to know what happens if network connectivity is off. Does the library handle itself or do i have to check the network connectivity before loading image to views?
My code:
Picasso picasso = Picasso.with(getActivity());
picasso.setDebugging(true);
picasso.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.into(eventImage, new Callback() {
#Override
public void onSuccess() {
Log.d("Success...", "picasso loaded successfully");
}
#Override
public void onError() {
Log.d("Error...", "picasso load error");
}
});
Using below code Picasso caches images for offline use.
Picasso.with(this)
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//use this for offline support
.into(eventImage);
Above code is not worke while removing cache.so Picasso can't find image from cache.If not get image from cache we handle to get image online and display it. We achieve that using below code:
Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//get error if image not loaded
}
});
}
});
Picasso caches images for offline use.
I'm using it in a simple movie app where I display a bunch of movie posters. I can turn on airplane mode and my images are still there. Likewise, if I force close the application while in airplane mode, then open the app again, my images will still load.
Hope this helps.
P.S. check out Glide https://github.com/bumptech/glide. It's faster and has smoother loading than Picasso

How to user Android-Volley to set animated image until the image loads

In android volley, I know there's an attribute called mDefaultImageId in the NetwrokImageView. But this only displays "Default" still images.
I need to show either an animated image or a progress bar.
I know I can accomplish this if I use RequestQueue, but still I need to take advantages of ImageLoader.
How to accomplish that?
Thanks.
You can create your own implementation of the ImageListener class and pass it when requesting the image.
Something along the lines of:
// code for displaying animation / progress wheel
sImageLoader.get(imageUrl, new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
// handle errors
}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
// code to switch out placeholder animation
// progress wheel with received response
}
}
});
You can try this: https://github.com/vinaysshenoy/enhanced-volley
there is Animation added to Volley.
also you can use
NetworkImageView ivImage = (NetworkImageView) rootView.findViewById(R.id.ivImage);
ivImage.setImageUrl(key, mImageLoader);
ivImage.setDefaultImageResId(R.drawable.loading_text);
where for me drawable loading_text was image saying "Loading image..."
if you are using method to load image then ProgressBar as parameter to that method may help you to hide ProgressBar:
https://stackoverflow.com/a/32666637/5045178

Categories

Resources