Firebase Android Series: Storage

Francisco García Sierra
ProAndroidDev
Published in
4 min readAug 9, 2018

--

Welcome to another article of Firebase Android Series. Here, we are going to learn how to build an Android app using Firebase from scratch.

This is the fourth article of Firebase Android Series. We put the focus on learning how to use Firebase to enhance an existing feature on your application or adding new ones.

The module that we are going to deal with this time is Firebase Storage.

Firebase Storage is a powerful, simple, and cost-effective object storage service. It includes Google security to file uploads and downloads for your Firebase apps.

Cloud Storage allows us to store images, audio, video, or other user-generated content. No matter the user’s connection, Firebase Storage SDK will automatically pause and resume your transfers as the app loses and regains mobile connectivity, saving your users time and bandwidth.

Firebase Storage integrates with Firebase Authentication to provide simple and intuitive authentication for developers. Continuing with this article series, we are going to use Firebase Storage to allow our chat users to upload images in the application. Let’s do this!

Configuring the project

Firebase Storage provides a declarative rules language that allows you to define how your data should be structured, indexed, and the read-write limitations over it. By default, these rules restrict read and write access to authenticated users. To get started without setting up Authentication, you can configure your rules for public access.

Storage References

Firebase Storage store your files in a Google Cloud Storage bucket. These files are organized in a hierarchical structure, just like the file system on your local hard disk. In the same way that Firestore or Realtime Database do, Firebase Storage uses references to represent these structure paths.

By creating a reference to a file, your app gains access to it. These references can then be used to upload or download data, get or update metadata or delete the file. A reference can either point to a specific file or to a higher level node in the hierarchy.

Firebase Storage references

Uploading data

In order to upload a file to Firebase Storage, first we need to create a reference to the full path of the file, including the file name. After creating our desired file reference, Firebase Storage provides us with different methods to upload our files to the cloud:

  • putBytes : Simplest way. Used to upload from data in memory. It takes a byte[] as parameter.
  • putFiles : Used to upload files from disk, such as photos or videos.
  • putStream : Most versatile method. It takes an InputStream as parameter.

All of the methods shown above return an UploadTask which is an object used to manage the upload of the given file. Firebase Storage uses Google Play Tasks API to manage all their calls. If you are not familiar with the Tasks API you can give it a look here.

Different ways to upload a file to Firebase Storage

In case you want to get the download URL of the file after completing the upload task, you will need to call getDownloadUrl over the original reference. A good practice is to concatenate Tasks to return the URL when your upload task has finished.

Upload file and retrieve the download url

Downloading data

Downloading data in Firebase Storage works the same way as uploading it. We just need to create a valid reference and make use of getBytes() , getFile() or getStream() to retrieve a valid FileDownloadTask to work with.

Different ways to upload a file to Firebase Storage

Finally, if you want to retrieve the download URL after downloading a file, you can make use of getDownloadUrl over your Task like we did with the uploading data example.

Note: When using getBytes() , Firebase must load the entire contents of your file into memory. If you request a file larger than your app’s available memory, your app will crash.

Deleting Data

To delete files in your cloud storage you just need to make a delete() over your file reference, in the same way that we do when using Firestore to delete our database references.

Metadata

After uploading a file to Firebase Storage, you can also retrieve and modify the file metadata, for example to view or update the content type. Files can also store custom key/value pairs with additional file metadata.

By default the metadata of a file contains common properties such as name, size , timeCreated and MIME type, together with other common values. This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method.

Retrieving custom metadata from a Firebase Storage file

In the same way that we retrieve the metadata we can update it making use of the updateMetadata() method.

The use of metadata is totally up to the developer and the project needs. It can be used to add more vital information to your files, custom types or even adding custom security keys on your Firebase Storage security rules to download data without Firebase Authentication.

Github Sample

This series will always work over the same project. Building a chat application using Firebase and Kotlin. You will find each one of the different articles’ code in individual branches of the project.

--

--