【问题标题】:How to load Image into ImageView from Url using Glide v4.0.0RC1如何使用 Glide v4.0.0RC1 从 Url 将图像加载到 ImageView
【发布时间】:2017-07-21 08:19:44
【问题描述】:

我刚刚在我的应用程序中将 Glide 库从 v3 更新到 v4。 但现在我无法从 url 加载图像。以前它在 v3 上运行良好。

这是我的 Glide 代码:

Glide.with(context).load(galleryList.get(itemPosition).getImage()).thumbnail(Glide.with(context).load(R.drawable.balls)).apply(options).into(holder.kolamImage);

v4 有什么变化?我浏览了文档,但仍然没有帮助。

【问题讨论】:

  • 尝试记录galleryList.get(itemPosition).getImage(),您也可以在.into()之前添加.listener()
  • 图片网址完美呈现。但是图像没有被 Glide 下载。我在onBindViewHolder()Adapter 中调用上述行。早些时候它使用 v3
  • 可能与缓存损坏有关

标签: android android-glide


【解决方案1】:

如果您使用的是Glide v4.0.0-RC1,那么您需要使用RequestOptions 来添加占位符、错误图像和其他选项。这是一个工作示例

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher_round)
                    .error(R.mipmap.ic_launcher_round);



 Glide.with(this).load(image_url).apply(options).into(imageView);

【讨论】:

  • glide 是否也缓存从互联网加载的图像?
【解决方案2】:
Glide.with(this)
        .load("url here") // image url
        .placeholder(R.drawable.placeholder) // any placeholder to load at start
        .error(R.drawable.imagenotfound)  // any image in case of error
        .override(200, 200) // resizing
        .centerCrop()     
        .into(imageView);  // imageview object

【讨论】:

    【解决方案3】:

    Glide v4 增加了RequestOptions 的功能,可以添加占位符、错误图片和自定义图片。

    RequestOptions options = new RequestOptions()
                        .placeholder(R.drawable.your_placeholder_image)
                        .error(R.drawable.your_error_image);
    
    Glide.with(this).load(image_url).apply(options).into(imageView);
    

    【讨论】:

      【解决方案4】:

      以下步骤是将图像从 URL 加载到 imageView 中:-

      像这样创建一个新的 Activity 并从给定的 url 加载图像。

      activity_main.xml

       <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
      
          <ImageView
              android:id="@+id/myOfferImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:adjustViewBounds="true"
              android:scaleType="fitXY" />
      
      </LinearLayout>
      

      MainActivity.java

      public class MainActivity extends AppCompatActivity {
      
      
          ImageView myOfferImageView;
          String url = "";
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
      
              url = "https://image.url"
      
      
              myOfferImageView = findViewById(R.id.myOfferImage);
              Glide.with(this).load(url)
                      .placeholder(R.drawable.ic_launcher_background)
                      .error(R.drawable.ic_launcher_background)
                      .into(myOfferImageView);
          }
      
      
      }
      

      【讨论】:

      【解决方案5】:

      确保您的 url 周围有引号,ivProfileImage 是您的图像视图。

      Glide.with(mContext)
          .asBitmap()
          .load("https://i2.wp.com/www.siasat.com/wp-content/uploads/2018/03/Rosamund-Pike.jpeg?fit=600%2C421&ssl=1")
          .into(ivProfileImage);
      

      【讨论】:

      猜你喜欢
      • 2017-12-17
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多