Retry loading image in Glide with 2 URL's

1

I am uploading an image with Glide, as follows.

Glide.with(myActivity.this).load(url1).diskCacheStrategy(DiskCacheStrategy.ALL).into(avatarImageView);

It turns out that sometimes fails me, since sometimes the url1 does not exist, and I have to load a url2 . (url1 and url2 is a String that corresponds to the URL of the image)

I want to know how to retry with the url2 if a url1 fails ...

Thank you!

EDIT: url1 and url2 are not null or empty. They have different formats when they were created and I must try with a format first and if it does not load, try with the other format.

Example:

String url1= "http://www.foto.xd/juan_perez_123456.jpg";
String url2= "http://www.foto.xd/123456.jpg";
    
asked by Maguz 14.08.2017 в 20:02
source

2 answers

1

What you need is a callback that determines if the load of the url fails, based on this load another image.

If an error is found you can use .error () and define an image of your resources.

But in this case you want to upload another image in case of error by means of a url, therefore you can load the image within onException() that is called if an error occurred when loading the first image:

    Glide.with(getApplicationContext())
            .load(url1)               
           // .error(R.drawable.androide_error)
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    // log exception
                    Log.e("Glide", "Error Cargando imagen!!! ", e);

                    //Procede a cargar otra imagen...
                    Glide.with(getApplicationContext()).load(url2)
                            .into(avatarImageView);

                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    Log.i("Glide", "Carga de imagen sin problemas!!!");
                    return false;
                }
            })
            .into(avatarImageView);

For more information you can see the official documentation .

    
answered by 14.08.2017 / 21:38
source
1

You can use the Glide callback and handle the error, in case the first image gives you an error, you reload it with Glide ...

Glide.with(myActivity.this).load(url1).
    diskCacheStrategy(DiskCacheStrategy.ALL).
    into(new SimpleTarget<Drawable>() {
    @Override
    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
        avatarImageView.setImageDrawable(resource);
    }
    @Override
    public void onLoadFailed(@Nullable Drawable errorDrawable) {
        //En el caso que falle url1  
        Glide.with(myActivity.this).
            load(url2).
            diskCacheStrategy(DiskCacheStrategy.ALL).
            into(avatarImageView);
    }
});
    
answered by 14.08.2017 в 21:38