image - Glide 文件未找到异常

标签 image kotlin filenotfoundexception loadimage

请注意

我想使用 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" />

谢谢:))

最佳答案

这解决了我的问题:

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

向 Glide 添加用户代理 header

完整代码:

@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>?) {
                    <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b5a9a8b281a3a8afa588aca0a6a4efb2a4b588aca0a6a485b3a0b6a0a3ada4" rel="noreferrer noopener nofollow">[email protected]</a>(resource)
                }
            })    }
}

关于image - Glide 文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69576936/

相关文章:

asp.net - 如何忽略全局 asax 中图像的日志记录错误

java - 在 JavaFX 中安全地关闭/删除 ImageView 文件

android - 无法生成签名的 apk,kotlin

java - Java 中的联合类型

android - 根据点击的数字打开对话然后 Intent 图像

java - Spring PropertySource 找不到属性

image - k-最近邻 VS 相似性搜索

java - 在 Image Swing 上旋转矩形

html - 表格列宽百分比编辑图片

c# - 使用 dll 方法时出现 FileNotFoundException