Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide: onError Callback

I'm switching from Picasso to Glide. Everything works fine except I cannot find a method to get an error callback. I want to retrieve a Bitmap, pass it on and generate an Android Palette from it. Also, while an errorDrawable can be provided to a load call, it won't show up in onResourceReady when using a SimpleTarget.

In Picasso I did it like this:

target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                //handle Bitmap, generate Palette etc. 
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                // use errorDrawable to generate Palette
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
            }
        };
        int width =  (int) DisplayUnitsConverter.dpToPx(this, 120);
        int height =  (int) DisplayUnitsConverter.dpToPx(this, 40);
        Picasso.with(this).load(config.getPathToLogo()).resize(width, height).error(errorDrawableId).into(target);

My glide code looks like this:

Glide.with(context)
    .load(config.getPathToLogo())
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(width, height) {
         @Override
         public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
             //handle Bitmap, generate Palette etc. 
         }
    });

Thanks.

like image 926
Androidicus Avatar asked Jun 28 '16 14:06

Androidicus


2 Answers

For everyone with the same problem - you need to use listener method. For example:

   Glide.with(activity)
            .load(getPhoto().getUrl())
            .apply(
                    new RequestOptions()
                            .error(R.drawable.icon_placeholder)
                            .centerCrop()
            )
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                 //on load failed
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    //on load success
                    return false;
                }
            })
            .transition(withCrossFade())
            .into(view);
like image 195
Patryk Kubiak Avatar answered Nov 08 '22 11:11

Patryk Kubiak


You are using SimpleTarget that implements the interface Target that defines the method onLoadFailed so you only need to do:

Glide.with(context)
    .load(config.getPathToLogo())
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(width, height) {
         @Override
         public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
             //handle Bitmap, generate Palette etc. 
         }

         @Override
         public void onLoadFailed(Exception e, Drawable errorDrawable) {
             // Do something.
         }
    });
like image 9
antonio Avatar answered Nov 08 '22 10:11

antonio