Glide:onError回调

时间:2016-06-28 14:43:37

标签: android android-glide

我从毕加索转向格莱德。一切正常,除了我找不到一个方法来获得错误回调。我想检索一个Bitmap,传递它并从中生成一个Android Palette。此外,虽然可以向加载调用提供errorDrawable,但在使用onResourceReady时,它不会显示在SimpleTarget中。

在毕加索,我这样做了:

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

我的滑动代码如下所示:

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

感谢。

2 个答案:

答案 0 :(得分:7)

您正在使用SimpleTarget来实现定义方法Target的接口onLoadFailed,因此您只需执行以下操作:

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

答案 1 :(得分:4)

对于每个有相同问题的人-您需要使用侦听器方法。例如:

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