Glide - 后退或调用加载照片时出错的调用方法

时间:2016-07-07 16:30:57

标签: android android-studio android-glide

Glide - 回退后调用方法或尝试加载照片时出错。

嗨!

有没有办法检查Glide是否从链接加载照片,或在链接无效或照片不可用时使用fallback / error

我的意思是,当Glide没有加载照片时,我想调用一种方法(加载其他照片)。

这是我的Glide例如:

Glide
        .with(mActivity)
        .load(news.getPagemap().getCseThumbnail().get(0).getSrc())
        .fallback(R.drawable.bg_gradient)
        .error(R.drawable.bg_gradient)
        .centerCrop()
        .crossFade()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);

我尝试比较ConstantValues - holder.photo.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable.bg_gradient).getConstantState())但得到NullPointerException

2 个答案:

答案 0 :(得分:9)

这有助于我:

private void loadPicture(final ViewHolder holder, String photoUrl, final Boolean shouldLoadAgain) {
    holder.progressBar.setVisibility(View.VISIBLE);

    Glide
        .with(mActivity)
        .load(photoUrl)
        .fallback(R.drawable.bg_gradient)
        .error(R.drawable.bg_gradient)
        .centerCrop()
        .crossFade()
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                if (shouldLoadAgain)
                    loadPicture(holder, mPhotoUrl, false);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);
}

答案 1 :(得分:0)

这对我有用。由于某些原因,如果加载时我不使用postDelayed失败,则该应用程序将崩溃。

private void updateImage(final String image) {
    Glide
            .with(this)
            .load(image)
            .apply(new RequestOptions()
                    .placeholder(R.drawable.bg_gradient)
                    .error(R.drawable.bg_gradient))
            .listener(new RequestListener<Drawable>() {

                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            updateImage(image);
                        }
                    }, 1000);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    return false;
                }
            })
            .into(holder.photo);
}