Android Upload Image to Firebase Storage Tutorial

In this tutorial you will learn to upload image to firebase storage in android.

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

Lets quickly learn how we can implement this in android.

Also Read: Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Upload Image to Firebase Storage

Adding Firebase to Project

First of all we have to connect our project to firebase and then add the required dependencies. For doing this just follow below steps.

Note: Below method of adding firebase will work only in android studio 2.2 and higher versions. If you are using any older version then follow this link to learn how to add firebase.

1. In android studio go to Tools > Firebase. It will open firebase assistant that allows us to add firebase to android studio within few clicks.

Android Upload Image to Firebase Storage Tutorial 1

2. Now you will see a window as shown below. Just click on Storage section and then click Upload and download a file with Firebase Storage.

Android Upload Image to Firebase Storage Tutorial 2

3. Click on Connect to Firebase, this will open a browser where you have to login to firebase using google account.

Android Upload Image to Firebase Storage Tutorial 3

4. After that you will see a window as shown below. Here you can create a new firebase project or use an existing one. In my case I am using an existing project. Finally click on Connect to Firebase.

Android Upload Image to Firebase Storage Tutorial 4

5. Now click on Add Firebase Storage to your app. In the next window click on Accept Changes to add all firebase storage related dependencies to android studio project.

Android Upload Image to Firebase Storage Tutorial 3

Android Upload Image to Firebase Storage Tutorial 5

Change Rules

By default we are not allowed to access firebase storage without authentication. To change it go to firebase console using any web browser. Open the firebase project that you have used in above steps and go to Storage and then Rules tab. Now change read and write rules to true as shown in below image.

Android Upload Image to Firebase Storage Tutorial 6

Android Project

Now comes the actual coding part. In this simple example I have given two button, one to choose the image from gallery and another to upload it.

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.

Below I have shared the full code for this.

Full Source Code

activity_main.xml

MainActivity.java

Now run and test your app.

Screenshots

Android Upload Image to Firebase Storage Tutorial

When you will login to firebase console the uploaded image will be shown something as given below.

Android Upload Image to Firebase Storage Tutorial 7

 

Comment below if you have any queries related to above android upload image to firebase storage example.

 

6 thoughts on “Android Upload Image to Firebase Storage Tutorial

  1. Shahrukh Ahmed

    Neeraj Mishra can you please share some tutorial about making a facebook like newsfeed in android using firebase

    Reply

Leave a Reply

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