Android,Firestore:GlideException:无法加载资源

时间:2020-10-05 09:43:26

标签: android firebase kotlin firebase-storage android-glide

我正在尝试从firebase storage加载图像并将其显示在Cardview中,但是它不起作用。我唯一得到的是:class com.bumptech.glide.load.engine.GlideException: Failed to load resourceOh, Something went wrong!(我自己的Timber消息)。这是我的代码

XML

<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="product"
            type="com.example.app.framework.datasource.models.product.Product" />
    </data>

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

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/mcv_product_item"
            android:layout_width="165dp"
            android:layout_height="210dp"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

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

                <ImageView
                    android:id="@+id/iv_product_image"
                    android:layout_width="165dp"
                    android:layout_height="110dp"
                    android:contentDescription="TODO"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:loadImage="@{product.images[0]}" <-- Trying to load Image here
                    tools:src="@drawable/ic_calibrate" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/tv_product_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="12dp"
                    android:layout_marginEnd="8dp"
                    android:ellipsize="end"
                    android:maxLines="2"
                    android:text="@{product.name}"
                    app:layout_constraintEnd_toEndOf="@+id/iv_product_image"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="@+id/iv_product_image"
                    app:layout_constraintTop_toBottomOf="@+id/iv_product_image"
                    tools:text="Test Name" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/tv_product_price"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="12dp"
                    android:text="@{product.price}"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/tv_product_name" />


            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.card.MaterialCardView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

数据类

data class Product(
    val name: String = "",
    val price: Float = 0F,
    val category: String = "",
    val note: String = "",
    val articelNumber: Int = 0,
    val images: ArrayList<String> = arrayListOf(),
    val description: ArrayList<ProductDescription> = arrayListOf(),
    val technicalDescription: ArrayList<ProductDescription>? = arrayListOf(),
    val hasAccessories: Boolean = false
)

data class ProductDescription(
    val category: String = "",
    val body: String = "",
)

滑行绑定适配器

@BindingAdapter("loadImage")
fun setImageFromUrl(view: ImageView, url: String?) {
    if (!url.isNullOrEmpty()) {
        GlideApp
            .with(view)
            .load(url)
            .fitCenter()
            .apply(RequestOptions.bitmapTransform(RoundedCorners(4)))
            .into(view)
    } else {
        Timber.d("Oh, Something went wrong!")
    }
}

GlideAppModule

@GlideModule
class GlideAppModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        super.registerComponents(context, glide, registry)
        registry.append(StorageReference::class.java, InputStream::class.java, FirebaseImageLoader.Factory())
    }
}

依赖项

implementation "com.firebaseui:firebase-ui-storage:6.3.0"
implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.11.0"
kapt "com.github.bumptech.glide:annotations:4.11.0"

堆栈跟踪(审查链接)

com.example.app W/Glide: Load failed for gs://censored.appspot.com/products/images/censored-SET_an238386.png with size [433x289]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
com.example.app D/BindingAdapterKt: Oh, Something went wrong!

我从cloud firestore获取了我的产品对象,并且除了相应的图像之外,cardview中的所有内容均已正确加载

感谢您的帮助。

编辑

不幸的是,在GlideApp中添加了一个侦听器并没有提供更多信息,

@BindingAdapter("loadImage")
fun setImageFromUrl(view: ImageView, url: String?) {
    if (!url.isNullOrEmpty()) {
        GlideApp
            .with(view)
            .load(url)
            .addListener(
                object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean,
                    ): Boolean {
                        if (e != null) {
                            Timber.d("Exception ist ${e.stackTraceToString()}")
                        }
                        return false
                    }

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

                }
            )
            .into(view)
    } else {
        Timber.d("Oh, Something went wrong!")
    }
}

错误仍然为class com.bumptech.glide.load.engine.GlideException: Failed to load resource。还可以从firebase cloud firestore

授予加载图像的权限

2 个答案:

答案 0 :(得分:0)

!!临时解决方案!

所以我找到了解决我问题的临时方法。而不是保存:

gs://censored.appspot.com/products/images/censored-SET_an238386.png

作为对图像的引用(这是正确的方法,但此处不起作用),请单击以提取https

enter image description here

然后您会得到类似的信息(在这里进行审查):

https://firebasestorage.googleapis.com/v0/b/censored.appspot.com
/o/products%2Fimages%2Fcensored-Leihgeraet_an326331.png?alt=media&token=censored-c92b-4153-a3f3-censored

将此https link保存在cloud-firestore数据库中,然后从那里获取它。与单独下载url链接相比,优点是上述方法更快。

但是,如上所述,这应该只是一个临时解决方案。我已经在Firebase-UI GitHub存储库中打开了一个Issue,我期待一个合适的解决方案

答案 1 :(得分:0)

它在 Java 中对我有用

    implementation 'com.firebaseui:firebase-ui-storage:3.2.1'
    implementation 'com.google.firebase:firebase-storage:19.2.1'
    implementation 'com.github.bumptech.glide:glide:4.11.0'

我在 Firestore RecyclerView 适配器中使用它

    String imgPath = "images/"+membersModel.getUid();
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference imageRef = storageRef.child(imgPath);
    imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            Glide.with(holder.VmemBpic.getContext())
                    .load(uri)
                    .placeholder(R.drawable.ic_user)
                    .circleCrop()
                    .error(R.drawable.ic_user)
                    .into(holder.VmemBpic);