0

I am trying to display images from Firebase's storage in my Android app inside a GridView.
I have done that, however, the images are displaying in an unknown order, and I have noticed that there is a 'Last modified' column inside Firebase's storage when I upload the images to it.

My question is: is there a way that I could sort the images inside the GridView in order to display them according to that date? (for example, the last one added, would have the latest 'Last modified' date and would be viewed first and such...)

This is my code:

val listRef : StorageReference = FirebaseStorage.getInstance().reference.child("images/posts/$userName")
    val  fileNameList: ArrayList<String> = ArrayList<String>()
    listRef.listAll()
        .addOnSuccessListener { it ->
            it.items.forEach{
                fileNameList.add(it.name)
            }
            gridView?.adapter = ImageRecyclerAdapter(activity, fileNameList,userName)
        }

Note: the code is in Kotlin

I have looked everywhere and couldn't find anything that helps.
Any help is appreciated, thank you :)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

1

According to the official documentation regarding StorageReference's listAll() method:

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

You might not be interested in listing folders within the reference you are pointing to.

That been said, the best option that you have is to store the URLs in a database. Such a database can be either Cloud Firestore or Firebase Realtime Database. This means that each object should have at least two fields, one for the actual URL and one for a timestamp. Please see in my answer from the following post how you can add a timestamp to Firestore:

Is in Java, but you can simply convert it to Kotlin. Once you have all URLs in place, you can create a query and order the URLs according to the date. In Firebase Realtime Database the default order is ASCENDING, but below is how you can reverse the order:

While in Firestore, you can simply pass the desired direction to Query's orderBy(String field, Query.Direction direction) method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193