Loading images from Firestore into a RecyclerView when there are multiple image types

非 Y 不嫁゛ 提交于 2020-01-16 12:33:28

问题


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() :

@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");
                }
            });
}

回答1:


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.

This is in case you don't have something like 'exist(name)' method in Firestore (simply I'm not familiar with it)



来源:https://stackoverflow.com/questions/58860133/loading-images-from-firestore-into-a-recyclerview-when-there-are-multiple-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!