com.bumptech.glide.load.engine.GlideException: 在 kotlin 中加载资源失败?

com.bumptech.glide.load.engine.GlideException: Failed to load resource in kotlin?

class com.bumptech.glide.load.engine.GlideException: Failed to load resource

class com.bumptech.glide.load.engine.GlideException: Failed to load resource in Android 10 only

enter image description here 我从 logcat 得到了错误。以上两篇文章我都看过了。我也用了他们介绍的方法。它仍然没有解决。我认为我的问题有不同的解决方案??

com.bumptech.glide.load.engine.GlideException: Failed to load resource
    There were 4 root causes:
    java.lang.RuntimeException(setDataSourceCallback failed: status = 0x80000000)
    java.lang.RuntimeException(setDataSourceCallback failed: status = 0x80000000)
    java.lang.RuntimeException(setDataSource failed: status = 0x80000000)
    java.lang.RuntimeException(setDataSource failed: status = 0x80000000)
     call GlideException#logRootCauses(String) for more detail

依赖关系

implementation "com.github.bumptech.glide:glide:4.12.0"
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

海洋节

<uses-permission android:name="android.permission.INTERNET"/>
android:usesCleartextTraffic="true"

代码

@BindingAdapter("imageUrl")
fun bindImage(imageView: ImageView,imgUrl:String?){
    imgUrl?.let {
        val imageUri=imgUrl.toUri().buildUpon().scheme("https").build()
        Glide.with(imageView.context)
            .load(imageUri)
            .centerCrop()
            .apply(RequestOptions()
                .placeholder(R.drawable.loading_animation)
                .error(R.drawable.ic_broken_image))
            .listener(object :RequestListener<Drawable>{
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Log.d("aa","===${e}")
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }
            })
            .into(imageView)
    }
}

布局

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="sport"
            type="com.example.retrofit2test.net.Sport" />
    </data>
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                app:imageUrl="@{sport.photo1}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:srcCompat="@tools:sample/avatars" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</layout>

这是因为您正在将图像 url 转换为您的设备上不存在的 Uri!如果你给 glide 加载一个 Uri,它会检查文件。由于显而易见的原因,它不存在。所以你不需要将它转换为uri。相反,您只需将 imageUrl 传递给它即可。 所以,替换

Glide.with(imageView.context)
        .load(imageUri)
        .centerCrop()
        .apply(RequestOptions()
            .placeholder(R.drawable.loading_animation)
            .error(R.drawable.ic_broken_image))
        .listener(object :RequestListener<Drawable>{
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>?,
                isFirstResource: Boolean
            ): Boolean {
                Log.d("aa","===${e}")
                return false
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable>?,
                dataSource: DataSource?,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
        })
        .into(imageView)

Glide.with(imageView.context)
        .load(imageUrl)
        .centerCrop()
        .apply(RequestOptions()
            .placeholder(R.drawable.loading_animation)
            .error(R.drawable.ic_broken_image))
        .listener(object :RequestListener<Drawable>{
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>?,
                isFirstResource: Boolean
            ): Boolean {
                Log.d("aa","===${e}")
                return false
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable>?,
                dataSource: DataSource?,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
        })
        .into(imageView)