Firebase Storage to upload and download image

Updated 27 December 2019

Save

Firebase Storage

Firebase storage provides facility to upload any file like image, video, audio, etc without using server side code.

In this blog, I’ll explain how to create in android application a feature that allows users to upload local image to firebase storage and download the list from the images with the help of firebase database       with an example.

 

Our example app lets user enter file name in a text field and upload image.

Step 1. Create a new project on android studio or open an existing project in which you want to add firebase storage.

Step 2. Add the firebase to the project, follow this link to setup firebase to project. Link: https://firebase.google.com/docs/android/setup

Step 3. Add these dependency to the build.gradle(app)

Step 4. Add the permission to the Androidmanifest.xml

Now Let’s Start the coding part

1. Firstly image is selected from gallery using file chooser and displayed in ImageView.

2. Before using firebase we have to get its instance and then using the instance create reference to firebase storage app. It can be done by following code.

3. Now a child reference is created and using it we call putFile() method to upload image to firebase storage.

4. If image is uploaded successfully then success message is shown otherwise error is shown in toast.

          5. To upload the image url to firebase database.

 

How to Upload the image

After Getting the image from the application we are now calling then uploadFile() method defined in the main activity to upload image to firebase storage.

 

In this firstly we are creating a child on the storage and using the that child reference to upload the image to firebase storage using putFile(imageUri!!). To get Success, failure and progress we using

    1. addOnSuccessListener
    2. addOnFailureListener
    3. addOnProgressListener

after successfully uploading the file to firebase storage we will be uploading the image url to the firebase database to do that we getting the name from the editText and image from the fileReference downloadurl and saving it to the firebase database by calling uploadFileToDatabase.

 

 

Full Source Code

       1. Let’s start with MainActivity.kt

          2. activity_main.xml :

        3. MainActivityHandler.kt : The handler will be used to handle the click on the button.

        4. To Save the link of the image to database we can use Upload.kt which will save the name of the image and it’s url

        5. After Uploading the image to firebase storage and firebase database Now to download all the images saved in the storage.

Create a class having name as ImagesActivity.kt : In this activity we are getting all the list of images saved in the firebase database.

        6. Layout of the activity_images.xml:

 

        7. In this we activity we are using the RecyclerView to show list of images. So we have to create the adapter to show the list of images downloaded from the firebase.

        8.  Layout for the list item having name “image_item.xml”

. . . . . . .

That’s all for now, I hope this blog have helped you, Comment below if you have any queries related to above android upload image to firebase storage example.

 

 

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home