【问题标题】:How to load my placeholder in case of failure to load url?如果无法加载 url,如何加载占位符?
【发布时间】:2019-07-10 21:50:53
【问题描述】:

我正在尝试使用 Glide 加载图像。 我给了它一个 url,但它导致 FileNotFound 异常。 但这不是我关心的问题,我关心的是我想加载一个占位符以防发生任何这些错误。 我按照指南使用滑翔,但没有任何反应。 请看我的代码。

我尝试添加 RequestListener 和错误回退。

       Glide.with(imageView.getContext()).load(url)      
       .transition(DrawableTransitionOptions.withCrossFade())
       .apply(new RequestOptions().placeholder(placeholder))
       .error(Glide.with(imageView.getContext()).load(placeholder))
       .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

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

我希望我的占位符加载到我的图像视图中。 实际结果是我的图像视图加载了一些我没有定义的占位符,表明图像链接被禁止或其他东西。 预期:我希望加载占位符。

【问题讨论】:

    标签: android error-handling drawable android-drawable android-glide


    【解决方案1】:

    试试这个:

    int placeholder = R.drawable.error 
    GlideApp.with(imageView.getContext())
              .load(url)
              .apply(new RequestOptions().placeholder(placeholder)
                  .error(placeholder).dontTransform()
                  .diskCacheStrategy(DiskCacheStrategy.ALL))
              .into(mImageView);
    

    【讨论】:

    • 转到你的ImageView in xml 在那里设置错误drawable。并检查您是否能够在布局预览中看到 drawable。我了解到您在 xml 中的 ImageView 存在问题。
    • 现在从那里删除并在 Glide 中设置 ..Glide.load(R.drawable.error) 像这样。看看它是否出现。
    【解决方案2】:

    您可以参考下面的代码以在出现错误时显示placeholder 图像:

     Glide.with(context).load(BASE_URL + imageUrl)
                        .apply(new RequestOptions().placeholder(R.drawable.defaultimage)
                                .error(R.drawable.defaultimage)).into(imageView);
    

    【讨论】:

      【解决方案3】:

      这里是修改后的代码:

      int placeholder = R.drawable.placeholder
      
      Glide.with(imageView.getContext())
                  .load(url)
                  .error(placeholder)
                  .placeholder(placeholder)
                  .into(imageView);
      

      【讨论】:

      • 您使用什么作为占位符?如果您不使用 RequestListener,请同时删除它并告诉我它是如何进行的
      • 也可以试试去掉crossFade,如果我没记错可能也是个问题
      • Rafz ,我的占位符是资源文件夹中的可绘制对象
      • 我明白,尝试删除 CrossFade 或不必要的 RequestListener,它应该可以工作
      • 我更新了答案。它应该可以工作,如果没有,那么可能是您的 Drawable 资源或 ImageView 本身有问题?
      【解决方案4】:

      试试下面这段代码。

      Glide.with(your context).load(imageurl).apply(RequestOptions.placeholderOf(your drawable here)).error(your error drawable).into(your imageview);
      

      希望它有效...

      【讨论】:

        【解决方案5】:

        试试这个-

        .error(getResourses.getDrawable(R.drawable.placeholder))
        

        而不是使用这个-

        .error(Glide.with(imageView.getContext()).load(placeholder))
        

        【讨论】:

          【解决方案6】:

          尝试使用 RequestOptions

          RequestOptions requestOptions = new RequestOptions();
          requestOptions.placeholder(R.drawable.ic_placeholder);
          requestOptions.error(R.drawable.ic_error);
          
          Glide.with(context)
               .load(url)
               .apply(requestOptions)
               .into(holder.imageView);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-24
            • 2020-07-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多