0

I'm trying to add pagination in images data that I'm retrieving from firebase using Firebase Storage. I have 10 images there and I want to display 2 at a time in RecyclerView and when the user scrolls down to end vertically, it loads the next 2 until all the images are displayed, I have also read some documentation of Firebase where it was mentioned to use storage.list(int max results) method but with that, it only shows the number of results that I pass in the method for instance if I pass 2 it shows 2 images only, and I can't load anymore. I've found one method too on the official documentation i.e below: https://firebase.google.com/docs/storage/android/list-files

public void listAllPaginated(@Nullable String pageToken) {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference listRef = storage.getReference().child("files/uid");

    // Fetch the next page of results, using the pageToken if we have one.
    Task<ListResult> listPageTask = pageToken != null
            ? listRef.list(100, pageToken)
            : listRef.list(100);

    listPageTask
            .addOnSuccessListener(new OnSuccessListener<ListResult>() {
                @Override
                public void onSuccess(ListResult listResult) {
                    List<StorageReference> prefixes = listResult.getPrefixes();
                    List<StorageReference> items = listResult.getItems();

                    // Process page of results
                    // ...

                    // Recurse onto next page
                    if (listResult.getPageToken() != null) {
                        listAllPaginated(listResult.getPageToken());
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // Uh-oh, an error occurred.
                }
            });
}

I'm confused about how to use it, I don't know where I can get a page token from in order to provide a reference to open the next page

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
D3mon
  • 91
  • 6
  • Have you tried to implement Firestore pagination as explained in the [official documentation](https://firebase.google.com/docs/firestore/query-data/query-cursors)? – Alex Mamo Apr 22 '22 at 07:25
  • the thing is I have 200 images in my firebase storage and I directly want to apply pagination to the urls that I'm retrieving from firebase storage – D3mon Apr 22 '22 at 07:40

1 Answers1

1

The thing is I have 200 images in my Firebase Storage and I directly want to apply pagination to the URLs that I'm retrieving from Firebase Storage.

To achieve that, you have two options. The first one would be to use StorageReference#listAll() method which:

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

And according to the official documentation regarding how to list files with Cloud Storage on Android, please note that:

Cloud Storage for Firebase allows you to list the contents of your Cloud Storage bucket. The SDKs return both the items and the prefixes of objects under the current Cloud Storage reference:

So you have to explicitly differentiate that (items vs. prefixes) in your application code and provide your own pagination algorithm.

The second option that you have, and the simplest one, in my opinion, would be to store the image URLs in the Firestore and implement the pagination as explained here:

Paginate data with query cursors

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • i appreciate your help but list all method that you mentioned it lists all the images at once while i wanted to achieve 10 images at once and when user reaches at the end of the recycler view it loads the next 10 images and as far as firestore method is concerned i know about this method but it is quite extensive in my case because i cannot store urls of 200 photos it will be quite a hectic task – D3mon Apr 26 '22 at 04:50
  • As I already mentioned, the best option that you have is to implement [pagination](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android). Give it a try and tell me if it works. Ok? – Alex Mamo Apr 26 '22 at 08:10
  • 1
    okay i'll give it a try and let you know if it works, thank you – D3mon Apr 27 '22 at 10:02
  • Ok, looking forward to seeing your feedback. – Alex Mamo Apr 27 '22 at 10:06
  • I totally forgot to write back, i implemented it with firestore and it works fine !! @Alex Mamo – D3mon May 31 '22 at 08:03
  • Good to hear that, D3mon. – Alex Mamo May 31 '22 at 08:11
  • yes I have added one more question regarding touch event in android here's the link: https://stackoverflow.com/questions/72444075/set-on-touch-listener-not-working-on-edit-text – D3mon May 31 '22 at 08:23
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo May 31 '22 at 08:26
  • So can I help you with other information regarding this question? – Alex Mamo May 31 '22 at 08:26
  • no sir thank you so much but i'll be grateful if you give my other question a look !! – D3mon May 31 '22 at 09:04