Open In App

Android: How to Upload an image on Firebase storage?

Last Updated : 07 Dec, 2021
Like Article

Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides secure file uploads and downloads for Firebase application.

This article explains how to build an Android application with the ability to select the image from the mobile gallery and upload images to Firebase Storage.

Here are the detailed steps:

  • Step 1. Create a new project on android studio or open an existing project in which you want to add authentication and add the firebase to that android application.
  • Step 2. Add the firebase storage dependency in build.gradle (Module:app)file. Latest Dependency for firebase storage is:
    implementation ''
  • Step 3. Setting up the activity_main.xml layout file
    The activity_main.xml layout file consists of:

    1. Two layouts: nesting linear layout inside relative layout
    2. Two buttons:
      • one for selecting an image from gallery
      • other button is for uploading an image on firebase storage on the cloud
    3. An image view in which image is shown chosen from the gallery

    Here is complete code for activity_main.xml:


    <?xml version="1.0" encoding="utf-8"?>
     <!--Linear Layout with horizontal orientation
         and other properties-->
        <!--Button for choosing image from gallery-->
            android:layout_height="wrap_content" />
        <!--Button for uploading image-->
            android:layout_height="wrap_content" />
        <!--Image View for showing image chosen from gallery-->
            android:layout_height="match_parent" />


  • Step 4. Setting up file
    In MainActivity

    • Set listeners on interaction of defined button views. On interaction, you want to call a method that triggers either the selection of an image from the gallery or the uploading of the selected image to Firebase storage. setOnClickListener is used for that action on interaction.
    • When SelectImage method is called, a new Intent instance is created. The intent type is set to image, and its action is set to get some content. The intent creates an image chooser dialog that allows the user to search through the device gallery to select the image from the gallery.
    • startActivityForResult is used to receive the result, which is the selected image.
    • To display this image, make use of a method called onActivityResult(). onActivityResult receives a request code, result code, and the data. Check in this method, if the request code equals PICK_IMAGE_REQUEST, with the result code equal to RESULT_OK and the data available. If all this is true, display the selected image in the ImageView below buttons.
    • Override the startActivityForResult method and write its implementation.
    • Also in uploadImage() method, Firebase storage reference is taken and putFile() function is used to upload the image to firebase storage with success and failure listeners. If an image is uploaded than success toast is there otherwise failure toast is there.

    package com.geeksforgeeks.uploadimagetofirebase;
    import android.content.Intent;
    import android.provider.MediaStore;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    import java.util.UUID;
    public class MainActivity extends AppCompatActivity {
        // views for button
        private Button btnSelect, btnUpload;
        // view for image view
        private ImageView imageView;
        // Uri indicates, where the image will be picked from
        private Uri filePath;
        // request code
        private final int PICK_IMAGE_REQUEST = 22;
        // instance for firebase storage and StorageReference
        FirebaseStorage storage;
        StorageReference storageReference;
        protected void onCreate(Bundle savedInstanceState)
            ActionBar actionBar;
            actionBar = getSupportActionBar();
            ColorDrawable colorDrawable
                = new ColorDrawable(
            // initialise views
            btnSelect = findViewById(;
            btnUpload = findViewById(;
            imageView = findViewById(;
            // get the Firebase  storage reference
            storage = FirebaseStorage.getInstance();
            storageReference = storage.getReference();
            // on pressing btnSelect SelectImage() is called
            btnSelect.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v)
            // on pressing btnUpload uploadImage() is called
            btnUpload.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v)
        // Select Image method
        private void SelectImage()
            // Defining Implicit Intent to mobile gallery
            Intent intent = new Intent();
                    "Select Image from here..."),
        // Override onActivityResult method
        protected void onActivityResult(int requestCode,
                                        int resultCode,
                                        Intent data)
            // checking request code and result code
            // if request code is PICK_IMAGE_REQUEST and
            // resultCode is RESULT_OK
            // then set image in the image view
            if (requestCode == PICK_IMAGE_REQUEST
                && resultCode == RESULT_OK
                && data != null
                && data.getData() != null) {
                // Get the Uri of data
                filePath = data.getData();
                try {
                    // Setting image on image view using Bitmap
                    Bitmap bitmap = MediaStore
                catch (IOException e) {
                    // Log the exception
        // UploadImage method
        private void uploadImage()
            if (filePath != null) {
                // Code for showing progressDialog while uploading
                ProgressDialog progressDialog
                    = new ProgressDialog(this);
                // Defining the child of storageReference
                StorageReference ref
                    = storageReference
                              + UUID.randomUUID().toString());
                // adding listeners on upload
                // or failure of image
                        new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            public void onSuccess(
                                UploadTask.TaskSnapshot taskSnapshot)
                                // Image uploaded successfully
                                // Dismiss dialog
                                              "Image Uploaded!!",
                    .addOnFailureListener(new OnFailureListener() {
                        public void onFailure(@NonNull Exception e)
                            // Error, Image not uploaded
                                          "Failed " + e.getMessage(),
                        new OnProgressListener<UploadTask.TaskSnapshot>() {
                            // Progress Listener for loading
                            // percentage on the dialog box
                            public void onProgress(
                                UploadTask.TaskSnapshot taskSnapshot)
                                double progress
                                    = (100.0
                                       * taskSnapshot.getBytesTransferred()
                                       / taskSnapshot.getTotalByteCount());
                                    "Uploaded "
                                    + (int)progress + "%");



  • Main Activity

    Main Activity containing buttons for choosing and uploading image

  • Choose image from gallery

    Main Activity with image view having image chosen from gallery

  • The uploaded image on firebase console:
    registered users

    registered users

  • Like Article
    Suggest improvement
    Share your thoughts in the comments

Similar Reads