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 …

firebase storage tutorial

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.

[sociallocker id=1372] Firebase Storage Tutorial Source Code [/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x