Kotlin Firebase Storage – Download Files to Memory, Local File | Android

In previous post, we had known the way to upload data. This tutorial shows you how to download Files to Memory, Local File with Firebase Cloud Storage.

Related Posts:
Kotlin Firebase Storage – Upload Data from Memory, Local File, Stream | Android
Kotlin Firebase Storage – Get List of Files example – Image List with FirebaseRecyclerAdapter | Android

I. How to download file

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

To know way to add Firebase Storage to Android App & enable Firebase Auth, please visit previous post:
Kotlin Firebase Storage – Upload Data from Memory, Local File, Stream | Android

This tutorial bases on the code from that post, and continues with download methods.

1. Create a Reference

2. Download Data using
2.1 getBytes()

getBytes() method is the easiest way to download a file, it must load the entire into memory.
If we request a file larger than available memory, the app will crash. So we should set the maximum size, or use another download method.

2.2 getFile()

When we wanna access the file while offline or to share the file in a different app, we use getFile(). It returns a DownloadTask to manage download and monitor the status of the download:

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.
(previous post’s functions)
– download file (image) that is uploaded recently using getBytes(), getFile() methods, then display it.


2. Technology

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

3. Project Structure

LoginActivity is for Authentication, then user can change to StorageActivity to upload/download 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.

– Upload a file first, then click any Download button:


III. Source code


By grokonez | December 25, 2017.

Last updated on July 13, 2018.

Related Posts

Got Something To Say:

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