Kotlin 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 with Kotlin.

Related Posts:
Kotlin Firebase Storage – Download Files to Memory, Local File | Android
Kotlin Firebase Storage – Get List of Files example – Image List with FirebaseRecyclerAdapter | 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 Add Firebase Storage

Steps to import and enable Firebase Storage is just like steps for Firebase Auth.
Just follow: Add_Firebase_to_Android_Project

0.2 Add Firebase Auth

By default, only authenticated users can upload or download 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:

0.3 Check

After adding Firebase Auth & Realtime DB, we can see:

build.gradle (project-level):

build.gradle (App-level):

google-services.json file:
kotlin-firebase-storage-json-file

1. 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:

2. Upload Data using
2.1 putBytes()

2.2 putFile()

2.3 putStream()

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.

kotlin-firebase-storage-upload-file-app-demo

2. Technology

– Gradle 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x

3. Project Structure

kotlin-firebase-storage-upload-file-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.kotlin.firebase.storage.
– Follow steps to add Firebase Auth and Firebase Storage.

4.2 Enable Firebase Auth

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

4.3 LoginActivity

In this tutorial, we don’t explain way to authenticate an user again. To know how to implement Firebase Authentication App Client, please visit:
Kotlin Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

4.4 StorageActivity

4.5 Android Manifest

4.6 Run & Check result

– Use Android Studio, build and Run your Android App:

kotlin-firebase-storage-upload-file-app-demo

– Open Firebase Project Console -> Storage:
kotlin-firebase-storage-upload-file-result-console

III. Source code

Kotlin-FirebaseStorage-upload-data

By grokonez | December 22, 2017.

Last updated on July 13, 2018.



Related Posts


Got Something To Say:

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

*