Firebase Storage – Upload Data from Memory, Local File, Stream | Android

Firebase Cloud Storage helps us upload and share rich content data. Data is stored in a Google Cloud Storage bucket. With Firebase, we can perform robust operations (download/upload) regardless of network quality with strong security (Cloud Storage integrates with Firebase Authentication) and high scalability.

In this tutorial, we’re gonna look at ways to upload data from Memory, Local file, Stream in an Android App using Firebase Storage.

More practice:
Firebase Storage – Download Files to Memory, Local File | Android
Firebase Storage – Get List of Files example – Image List | Android

I. How to upload file

To use the Firebase Storage to upload data, we need:
– add Firebase to Android App & enable Firebase Auth
– create a reference to the full path of the file, including the file name
– upload data using putBytes() for in-memory data, putStream() for stream data, putFile() for local file.

0. Add Firebase to Android App
0.1 Create Firebase Project and Add Firebase Config file

Follow this guide to create Firebase Project and generate google-services.json file and move it into your Android App root directory. You don’t need to get SHA-1 Key in this example.

0.2 Add dependencies

build.gradle (project-level):

build.gradle (App-level):

1. Enable Firebase Auth

By default, only authenticated users can read or write data, so we need Firebase Authentication for next step.
Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password.

To do without setting up Authentication, we can change the rules in the Firebase Console -> choose Project -> Storage section on the left -> Rules tab:

2. Create a Reference

We cannot upload data with a reference to the root of Google Cloud Storage bucket. Reference must point to a child URL:

If the file names are the same:

3. Upload Data using
3.1 putBytes()

3.2 putStream()

3.3 putFile()

3.4 Manage Uploads

We can pause, resume, or cancel upload process:

II. Practice

1. Goal

We will build an Android App that can:
– create Account, sign in/sign out for Firebase Authentication.
– choose image from Gallery, then upload it to Firebase Cloud Storage using putBytes(), putStream() and putFile() methods.

firebase-storage-app-demo

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure

firebase-storage-structure

LoginActivity is for Authentication, then user can change to StorageActivity to upload image to Firebase Cloud Storage.

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.firebasestorage.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.firebasestorage.

firebase-storage-add-app

4.3 Enable Firebase Auth

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password.

4.4 LoginActivity

To know how to implement Firebase Authentication App Client, please visit:
Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

In this tutorial, we don’t explain way to authenticate an user again.

4.5 StorageActivity

4.6 Android Manifest

4.7 Run & Check result

– Use Android Studio, build and Run your Android App:
firebase-storage-app-demo

– Open Firebase Project Console -> Storage:
firebase-storage-demo-console

III. Source code

FirebaseStorage

By JavaSampleApproach | August 17, 2017.


Related Posts


4 thoughts on “Firebase Storage – Upload Data from Memory, Local File, Stream | Android”

  1. it helped a lot thanks
    now i have to find something that doens’t need Login or anything the tutorials i’ve found are for sdk 24.0.0 version and i don’t know why don’t matter what I do it doesn’t works out

    1. Hi Joy,

      If you want to work with Firebase Storage without Authentication (Login), you can go to your Project in Firebase Console, chose Storage tab -> RULES, change the rules:

    1. Hi Scott,

      putBytes() would be the fastest method because it holds the entire contents of a file in memory at once.

      Regards,
      JSA.

Got Something To Say:

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

*