Download files from Cloud Storage
In our last section, we learned the different ways of uploading files. Now, we will learn how to download the files from the Cloud Storage. For downloading a file, first, we have to create a Cloud Storage reference to the file which we want to download.
There are following two ways through which we can create a reference, i.e., by appending child paths to the storage root and from an existing gs:// or https://URL referencing an object in the Cloud Storage.
Downloading Methods
After creating an appropriate reference, we call various methods to download files, i.e., getBytes(), getStream(), or we can get a download URL with getDownloadUrl() for downloading the file with another library.
The use of the getBytes() method is the easiest way of downloading the file to memory. If we request a file larger than our app’s available memory, our app will crash. The getBytes() takes a maximum amount of bytes to download. We set the maximum size according to our app capability.
Downloading Data to a Local File
If we want to have access to the file while offline or to share the file, then we use the getFile() method. This method downloads a file directly to a local device. This method returns a DownloadTask, which we can use to manage our downloads and monitor the status.
Downloading Data via URL
If we already have the download infrastructure based on the URL, or simply want to share a URL, we can get the download URL for the file by calling the getDownloadUrl () method on the storage context.
Downloading images with FirebaseUI
For eliminating boilerplate code and promoting Google best practices. FirebaseUI provides simple, customized, and production-ready native mobile bindings. Using FirebaseUI, we can quickly, easily download, cache, and display images from cloud storage using our integration with Glide.
First, add FirebaseUI to our app/build.gradle:
Then we can load images directly from storage into an ImageView: