Stelios Papamichail
Stelios Papamichail

Reputation: 1270

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

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

Upvotes: 0

Views: 81

Answers (1)

Oleksandr Martynets
Oleksandr Martynets

Reputation: 136

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)

Upvotes: 1

Related Questions