有多种图像类型时,将图像从Firestore加载到RecyclerView中 [英] Loading images from Firestore into a RecyclerView when there are multiple image types
问题描述
我有一个RecyclerView,它使用GridLayoutManager,并且每个单元格都包含一个ImageView.我想用来填充的数据以.png和.jpeg图像的形式存储在Firestore存储中,每个图像的命名为 icon#.png
或 icon#.jpeg
其中#是相应单元格的位置.我的问题是,当我尝试按照下面所示的方式将每个图像加载到各自单元格的imageView中时,收到一条错误消息,因为由于某些扩展名为.png而另一些具有.png扩展名,因此我无法检索到所有图像.jpeg一个.据我了解,我需要先将文件扩展名传递到 storageReference.child()
方法中,然后才能从Firestore加载任何图像,对吗?那么我如何在这里实现我的目标?
I have a RecyclerView which uses a GridLayoutManager and each cell consists of a single ImageView. The data that i want to use to populate it are stored in my Firestore storage as .png and .jpeg images and the naming of each one is icon#.png
or icon#.jpeg
where # is the respective cell's position. My issue is that when i try to load each image into the respective cell's imageView with the way that you see below, i get an error message because i can't retrieve every image due to some having a .png extension while others have a .jpeg one. It's my understanding that i need to pass the file extension into the storageReference.child()
method before i can load any image from Firestore, correct? So how can i achieve my goal here?
onBindViewHolder():
onBindViewHolder() :
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
storageReference.child("Icons/icon"+position+".png").getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
GlideApp.with(mContext).load(uri).into(holder.iconImgView);
Log.d("Adapter", "Image loaded successfully");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("Adapter", "ERROR DOWNLOADING IMAGE");
}
});
}
推荐答案
不是最聪明的方法,但更简单的方法是在 onFailure
方法中获取* .png,并为*运行相同的提取.jpeg.而且只有在处理错误之后.
Not the smartest one but the easier method is to in onFailure
method for fetching of*.png run the same fetch for *.jpeg. And only after that handle error.
以防万一,您在Firestore中没有类似'exist(name)'的方法(可能是我不熟悉)
This is in case you don't have something like 'exist(name)' method in Firestore (simply I'm not familiar with it)
这篇关于有多种图像类型时,将图像从Firestore加载到RecyclerView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!