Firebase Storage Tutorial for Android – Upload Files in Firebase Storage

Hello guys, welcome to Firebase Storage Tutorial for Android. In this tutorial we are going to learn how you can upload images to firebase storage. Infact not only images you can use this firebase storage android tutorial to upload any kind of file to firebase storage. So lets start our Firebase Storage Tutorial.

Follow Updated Tutorial from this Link
Firebase Storage Example to Upload and Retrieve Images

Firebase Storage Tutorial Video

You can go through this Firebase Storage Tutorial Video if you don’t like reading text tutorial. The video is covering everything you will find in this post.

But if you are ok with written post then lets move ahead in this Firebase Storage Tutorial.

Starting Firebase Storage Tutorial Project

Creating a new Project

  • As always the first step is creating a new Android Studio Project.
  • So just create a new Android Studio project using an Empty Activity. I created a project named FirebaseStorage.
  • Once your project is loaded, you can add Firebase storage to it.

Adding Firebase Storage

  • With new Android 2.2 it is really easy to integrate firebase. (If you haven’t updated your studio, you should update your Android Studio).
  • To add Firebase Storage, click on Tools -> Firebase

firebase storage

  • An assistant window would open in left with all the firebase features. We have to select Firebase Storage from the list.

firebase storage

  • Now you will see a link saying Upload and Download a File with Firebase Storage click on it.

firebase file upload

  • Now you will see again the same screen we seen in the last Firebase Cloud Messaging Tutorial. You have to do the same things.

#1 Connect your app to firebase

  • Click  on Connect to Firebase. You will see a dialog asking to create a new firebase app or choose an existing one.

connect to firebase project

#2 Adding Firebase Storage

  • Now Click on the second button Add Firebase Storage to Your App. 

firebase storage tutorial

  • Then click on accept changes and firebase storage is added.

firebase storage tutorial

Getting a File to Upload

  • If we need to upload a file, then the first step is getting that file.
  • For this we will create a File Chooser.

Creating File Chooser

  • First we will create layout for our file chooser.

Creating Layout

  • Come inside activity_main.xml and write the following code.

  • The above xml code will generate the following layout.

firebase storage

  • Now we will code the functionality to the choose button. When we tap the choose button Image Chooser should open. So lets do it.

Coding File Chooser

  • Come inside MainActivity.java, and write the following code.

Testing File Chooser

  • Now you can run your application to check whether the file chooser is working or not.

file chooser activity

  • As you can see it is working absolutely fine. So now we can move ahead to learn the main topic of this Firebase Storage Tutorial, which is uploading a file.

Uploading Selected Image

  • We have the chosen image. Now when the user will tap the upload button the file should be uploaded to Firebase Storage.

Coding Upload Method

  • So inside MainActivity class create a method named uploadFile() and write the following code for it.

  • Now call this method when the button upload is clicked.

  • If you will try running the application it will not work, because of the default Storage Rules.

Changing the Default Rules

  • Because the default rules says, only authenticated users will be able to read or write the Firebase Storage. But we haven’t done any authentication in our application.
  • So for now we are changing the Firebase Storage Rules.
  • So go to Firebase Console and open your Firebase Project. Then from the left menu select Firebase Storage and go to the Rules tab.

firebase storage example

  • You can see I have changed the rule. So you have to change the rule as shown above. Before it was if auth != null but I changed it to if true so the if will always evaluate true.
  • But you should use this only for development purpose. As for production you cannot use this way that anyone can access storage. 

Testing the Upload

  • Now just run your application.

firebase storage upload image

  • You can also check the firebase storage to check whether the file is uploaded or not.

firebase storage uploads

  • As you can see we have the file here in Firebase Storage.
  • If you are facing troubles get my code from the below link.

So thats all for this Firebase Storage Tutorial friends. In next post we will learn about creating an image storing application using Firebase Storage. If you are having queries lets meet in comment section. Thank You 🙂

33 thoughts on “Firebase Storage Tutorial for Android – Upload Files in Firebase Storage”

    • Hi I am also searching for that video.
      But I am still not able to find any video regarding uploading the pdf files. If you have got one by now plz share or else whenever you get plz revert back.

      Reply
      • i have done upload pdf files dude , the main idea is only change this code

        intent.setType(“file.pdf/*”);

        and

        StorageReference riversRef = storageReference.child(“file/.pdf”);
        riversRef.putFile(filePath)

        it will make file folder on console firbase storage

        but i dont know how to upload many files , so i just can upload 1 file 🙁

        Reply
  1. hello sir , if we use this code for chat purpose , can we use this code for profile pic ………., if yes how we use upload image as profile pic ….

    Reply
  2. Sir i have one question
    Can i use firebase in Eclipse IDE if yes than where to add dependance classpath file.

    In Android studio we can add classpath in gradle scripts folder but where in Eclipse.

    Reply
  3. Help me!
    I edit on rule fire base->read,write:if true;
    But it doesn’t work
    Error is:”an unknown error occurred please check the http result code and inner exception for server response”

    Reply
  4. FOR ERRORS OF This method should only be accessed from tests or within private scope
    use

    @SuppressWarnings (“VisibleForTests”)
    double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

    Reply
  5. Everything seems working properly, except one thing
    it keeps give me this error :

    StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzbqn: Please sign in before trying to get a token.

    Shall i be signed in using my gmail account in the emulator to get it working ?

    Reply
  6. Very well done.. and explained.
    But i still feel you should show us how to download that image from Firebase storage when needed.

    I have seen some tutorials with GLIDE, but would like to see your version. Thanx!

    Reply
  7. thanks for the tutorial, but why when i click the upload button there is a notification User dont have permission to access this object. Please help me to resolve this problem

    Reply
  8. For Those Who are getting Red Lines in Studio After copying this code
    Change This
    StorageReference riversRef = storageReference.child(“images/pic.jpg”);
    to
    StorageReference riversRef =FirebaseStorage.getInstance().getReference().child(“images/pic.jpg”);

    Reply
  9. 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.

    Reply
  10. Im getting this error Please help me out
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
    Code: -13000 HttpResult: 0

    Reply

Leave a Comment