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.

 

16 thoughts on “Android Upload Image to Firebase Storage Tutorial”

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

  2. Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

    Hahahah it Resizes your Images ,

  3. SAGAR M TAHELYANI

    Hello, Good tutorial about that.

    I have a question regarding this. If I started to upload an image and there is no internet connectivity while uploading. Then onFailure is not called at that time. So how I know image uploading failure?

    Remember: When I start to upload, the internet is there. but while uploading process, the net is not there. If you have any idea about that. please respond to this query.

  4. I experiencing an error on this line
    StorageReference storageRef = storage.getReferenceFromUrl(“gs://pictureupload-db92f.appspot.com/images”);

  5. i tried this but it didnt work either

    StorageReference storageRef = storage.getReferenceFromUrl(“gs://pictureupload-db92f.appspot.com”);

Leave a Comment

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