【问题标题】:Glide FileNotFoundExceptionGlide FileNotFoundException
【发布时间】:2021-10-14 20:23:59
【问题描述】:

سلام عليكم

我想用 Glide 加载图像,但我收到了这个错误:

java.io.FileNotFoundException: https://via.placeholder.com/600/1e5390
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:102)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:56)
        at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:310)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:276)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)

这是图片网址: https://via.placeholder.com/600/1e5390

这是 Glide 代码:

@BindingAdapter("imageUrl")
fun ImageView.bindImage( imgUrl: String?) {
        imgUrl?.let {
            val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
            Glide.with(this.context)
                .load(imgUri)
                .apply(
                    RequestOptions()
                        .error(R.drawable.ic_broken_image)
                )
              
                .into(this)

    }
}

在这里使用它:

<androidx.appcompat.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            app:imageUrl="@{photoObject.photoUrl}"
            app:srcCompat="@drawable/ic_broken_image" />

谢谢你:))

【问题讨论】:

  • 我猜,问题出在图片网址上。我尝试使用 2 个图像 url 并且工作正常。如果你的 Web 服务器在那个 URL 上提供一个图像,并且具有一个有效的图像 MIME 类型,Glide 会处理它。可能是您的服务器没有提供有效的图像 MIME 类型。您可以查看此stackoverflow.com/questions/58296248/… 了解更多信息。谢谢。

标签: image kotlin filenotfoundexception loadimage


【解决方案1】:

这解决了我的问题:

val theImage = GlideUrl(
        imgUrl, LazyHeaders.Builder()
            .addHeader("User-Agent", "5")
            .build()
    )

将用户代理头添加到 Glide

完整代码:

@BindingAdapter("imageUrl")
fun ImageView.bindImage( imgUrl: String?) {


    val theImage = GlideUrl(
        imgUrl, LazyHeaders.Builder()
            .addHeader("User-Agent", "5")
            .build()
    )

    theImage.let {
        Glide.with(this.context)
            .load(theImage)
            .apply(
                RequestOptions()
                    .error(R.drawable.ic_broken_image)

            )
            .into(object : CustomViewTarget<ImageView, Drawable>(this) {
                override fun onLoadFailed(errorDrawable: Drawable?) {}
                override fun onResourceCleared(placeholder: Drawable?) {}
                override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                    this@bindImage.setImageDrawable(resource)
                }
            })    }
}

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 2013-12-09
    • 2011-06-09
    • 2016-04-05
    • 2018-07-11
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多