Firebase Storage – Get List of Files example – Image List | Android

We had known the way to upload/download data. In this tutorial, we’re gonna look at way to get list of files example – display list of Images with Firebase UI Database FirebaseRecyclerAdapter.

Related Posts:
Firebase Storage – Upload Data from Memory, Local File, Stream | Android
Firebase Storage – Download Files to Memory, Local File | Android

I. How to do

firebase-storage-display-list-goal

To display list of Images, we need to:
– add Firebase to Android App & enable Firebase Auth
– use Firebase Cloud Storage to upload and store images
– use Firebase Realtime Database to store images’ information (name + url)
– use FirebaseRecyclerAdapter to display images in RecyclerView with the help of Picasso lib.

To know way to add Firebase to Android App & enable Firebase Auth, please visit previous post:
Firebase Storage – Upload Data from Memory, Local File, Stream | Android

1. Upload and store images

Use Firebase Cloud Storage:

firebase-storage-display-list-demo-storage-console

2. Store images’ information

Use Firebase Realtime Database:

firebase-storage-display-list-demo-database-console

3. Display list of Images
3.1 Model and ViewHolder

Model class is a class that represents the data from Firebase:

ViewHolder layout (R.layout.item_image) with UI items that correspond to Model fields:

ViewHolder class contains Android UI fields that point to layout items:

3.2 FirebaseRecyclerAdapter subclass

We need a subclass of the FirebaseRecyclerAdapter and implement its populateViewHolder() method:

Now look at these lines of code:

– We tell FirebaseRecyclerAdapter object to use UploadInfo.class when reading from the database.
– Each Message will be displayed in a R.layout.item_image (that has 2 elements: tv_img_name, img_view).
– We indicate class for ViewHolder
– We can just give reference to database node or sort/filter data by using Query:

FirebaseRecyclerAdapter will call populateViewHolder() method for each Model it finds in database. It passes us the Model and a ViewHolder.
So what we should do is map the fields from model to the correct UI elements:

3.3 RecyclerView

Now we set the adapter for RecyclerView object to provide child views on demand:

Remember to call adapter cleanup() method to stop listening for changes in the Firebase database:

4. Depedencies

To add Support Library to Android project, open the build.gradle file (project-level).
Make sure that the repositories section includes a maven section with the “https://maven.google.com” endpoint:

For build.gradle file (App-level)

II. Practice

1. Goal

We will build an Android App that can:
– create Account, sign in/sign out for Firebase Authentication.
– read/write user to Firebase Realtime Database.
(2 lines above come from this Post).
– upload image to Firebase Cloud Storage (from this Post)
– display list of Images using FirebaseUI FirebaseRecyclerAdapter.

firebase-storage-display-list-demo-app-goal

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x
– Firebase UI Database 2.1.1
– Picasso 2.5.2

3. Project Structure

firebase-storage-display-list-structure

LoginActivity is for Authentication, then user can enter StorageActivity to upload Image to Firebase Storage, Image’s information to Firebase Database, and show list of Images.

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.firebaserealtimedb.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.firebasestorage.

firebase-storage-add-app

4.3 Enable Firebase Auth

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password.

4.4 Model

4.5 LoginActivity

To know how to implement Firebase Authentication App Client, please visit:
Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

In this tutorial, we don’t explain way to authenticate an user again.

4.6 ViewHolder

4.7 StorageActivity

4.8 Android Manifest

4.9 Run & Check result

– Use Android Studio, build and Run your Android App:

firebase-storage-display-list-demo-app-result

– Firebase Console:
+ Storage:
firebase-storage-display-list-demo-storage-console

+ Database:
firebase-storage-display-list-demo-database-console

III. Source code

FirebaseStorage-display-list-images


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*