0

So I am currently using FirestoreRecyclerAdapter to display images from the Firebase Storage via the Firestore database, that is

Firebase Storage file url > Firestore database url > RecyclerView (FirestoreRecyclerAdapter)

This is fine for a small number of images. But when dealing with 150 images per set, this is starting to become a hassle.

Is there a more efficient way of dealing with this? Like looping through all the images from the Firebase Storage and displaying them directly onto the RecyclerView?

Kindly point me to a tutorial if it exists, preferably Kotlin. I'm not really with docs. I know I'm already asking for too much. Thanks!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Kellin Strook
  • 487
  • 1
  • 5
  • 18

2 Answers2

0

One way to improve the efficiency of displaying images in a RecyclerView is to use a library that handles image loading and caching. Glide and Picasso are two popular options for this. Both libraries can load images directly from Firebase Storage using the StorageReference object and can cache the images to improve performance.

Maybe some code like that would work:

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val imageRef = getItem(position).imageRef // get the StorageReference for the image
    Glide.with(context)
        .load(imageRef)
        .into(holder.imageView)
}

But it needs some setup, please check it here in this answer: https://stackoverflow.com/a/48762436/7334951

Younes Charfaoui
  • 1,059
  • 3
  • 10
  • 20
  • Hey thanks. I currently use Glide with FirestoreRecyclerAdapter. I have just started learning programming so I'll study this for a bit. Thanks again. – Kellin Strook Mar 19 '23 at 13:18
0

As I understand from your question, you have added some images in Cloud Storage and the corresponding URLs in Firestore. In order to display the images from Cloud Storage in a RecyclerView, you read the URLs from Firestore first. This practice is quite common when you want to store details about the files you're working with. However, if you only want to display the images from Storage, there is no need to read the URLs from Firestore.

Is there a more efficient way of dealing with this? Like looping through all the images from the Firebase Storage and displaying them directly onto the RecyclerView?

Yes, there is. You can use StorageReference#listAll() which:

List all items (files) and prefixes (folders) under this StorageReference.

This means that you can call the above method to list all files that exist at a particular location in your Storage bucket. In code, it will be as simple as

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Kellin. Have you tried my solution above, does it work for your use case? – Alex Mamo Mar 15 '23 at 06:42
  • Hi, i'm sorry I wasn't clear enough. I currently display images this way. Firebase Storage > Cloud Firestore > FirestoreRecyclerAdapter > Glide. I was wondering if it's possible to display the image directly from the Firebase Storage into the RecyclerView without pasting the url into Cloud Firestore. – Kellin Strook Mar 19 '23 at 13:15
  • Yes, that's perfectly possible. Have you tried the solution in my answer and the code in the other [answer](https://stackoverflow.com/questions/75344799/is-it-necessary-to-create-151-connections-to-the-server-when-displaying-150-imag)? – Alex Mamo Mar 20 '23 at 06:34