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 Post:
Firebase Storage – Upload Data from Memory, Local File, Stream | Android

More Practice:
Firebase Storage – Get List of Files example – Image List | 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 to Android App & enable Firebase Auth, please visit previous post:
Firebase Storage – Upload Data from Memory, Local File, Stream | Android

This tutorial bases on the code from that post, and continue 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.

firebase-storage-download-file-demo-app

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/download 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-download-file-demo-app

III. Source code

FirebaseStorage-download-file

By JavaSampleApproach | August 18, 2017.


Related Posts


Got Something To Say:

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

*