使用Glide首次加载时,Imageview没有图像

时间:2019-04-18 06:02:52

标签: android android-recyclerview recycler-adapter android-glide httpexception

我正在使用Glide v4加载图片,而我的应用遇到错误:

I/Glide: Root cause (1 of 1) com.bumptech.glide.load.HttpException: Not Found
    at com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher.onResponse(OkHttpStreamFetcher.java:73)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)

这是第一次加载时发生的情况,但是当我再次重新加载时,图像现在出现了。

我正在从RecyclerView适配器加载图像,例如:

class MyRecyclerViewAdapter(private val glideApp: GlideRequests): RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>() {
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //... some stuff
        // Set a data
        glideApp.asBitmap()
            .load(img_url)
            .into(holder.itemView.ivImageHolder)
        // Set a data
    }
}

感谢您的帮助,谢谢。

3 个答案:

答案 0 :(得分:0)

尝试一下

私人列表myUrls = ...;

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
  ImageView imageView = ((MyViewHolder) viewHolder).imageView;
  String currentUrl = myUrls.get(position);

  Glide.with(fragment)
    .load(currentUrl)
    .override(imageWidthPixels, imageHeightPixels)
    .into(imageView);
}

check this also

答案 1 :(得分:0)

Glide.with(context)
    .load(img_url)
    .asBitmap()
    .into(holder.itemView.ivImageHolder);

“上下文”包含传递到适配器的活动或片段的实例

答案 2 :(得分:0)

我通过更改 ImageView 属性解决了该问题。

android:width:"wrap_content"更改为android:width:"match_parent"