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

Posted

技术标签:

【中文标题】Android,Firestore:GlideException:无法加载资源【英文标题】:Android, Firestore: GlideException: Failed to load resource 【发布时间】:2021-01-20 04:10:44 【问题描述】:

我正在尝试从我的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_
        android:layout_>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/mcv_product_item"
            android:layout_
            android:layout_
            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_
                android:layout_>

                <ImageView
                    android:id="@+id/iv_product_image"
                    android:layout_
                    android:layout_
                    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_
                    android:layout_
                    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_
                    android:layout_
                    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 = "",
)

Glide 绑定适配器

@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

【问题讨论】:

删除fitCenter()并尝试 @ADM 完全没有区别,仍然失败。在我看来,这与size of the image viewsize of the image 本身有关 ImageView 大小应该不是问题。首先尝试最小化问题。删除fitCenter() 并应用转换并确保您的设备上有互联网。为 Glide 图像加载方法添加监听器并调试 Exception @ADM 已经删除了fitCenter().apply(/...),完全没有区别。向 Glide 添加一个监听器绝对没用,因为它告诉我相同的 GlideException: Failed to load resource。 Internet 连接始终可用,否则它将永远无法获得storage reference 和所有其他product details。我也cleaned Build and Invalidated Caches,没有任何帮助。 【参考方案1】:

!!临时解决方案!

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

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

作为您对图像的引用(这是正确的方法,但在这里它不起作用),通过单击提取 https

然后你会得到类似的东西(这里被审查):

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 存储库中打开了一个问题,我期待找到合适的解决方案

【讨论】:

好吧,我之前使用过 Firestore,然后我只保存文件名,即 censored-SET_an238386.png 而不是 Url,它与 Glide 配合得很好。您是否尝试过仅使用文件名? @ADM 看起来这可能是解决方案。唯一的问题是,我得到了java.io.FileNotFoundException: /censored_an238386.png: open failed: ENOENT (No such file or directory)。我的文件存储在一些文件夹中,我应该保存整个根目录吗? 可以试试 @ADM 不,不工作,你知道完整路径是什么吗?我只收到/product/images/myfolder/censored-SET_an238386.png,但我认为/product前面需要有一些东西【参考方案2】:

它适用于我在 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);

【讨论】:

以上是关于Android,Firestore:GlideException:无法加载资源的主要内容,如果未能解决你的问题,请参考以下文章

Firebase Firestore:无法访问Firestore后端(仅限Android)

Firestore分页与FirestoreRecyclerAdapter(Android)[重复]

Android 上的 Firestore 商店缓存在哪里?

Android 应用的 Firestore 安全设置应该是啥? [关闭]

Firestore返回null反应原生android

在 Firestore 中删除集合 - Flutter/Android