Firebase Storage allows developers to quickly and easily download files from a Google Cloud Storage bucket provided and managed by Firebase.
Create a Reference
To download a file, first create a Firebase Storage reference to the file you want to download.
You can create a reference by appending child paths to the storage root, or you
can create a reference from an existing gs://
or https://
URL referencing
an object in Firebase Storage.
// Create a storage reference from our app StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-bucket-name>"); // Create a reference with an initial file path and name StorageReference pathReference = storageRef.child("images/stars.jpg"); // Create a reference to a file from a Google Cloud Storage URI StorageReference gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference httpsReference = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
Download Files
Once you have a reference, you can download files from Firebase Storage
by calling the getBytes()
or getStream()
. If you prefer to download the file
with another library, you can get a download URL with getDownloadUrl()
.
Download in memory
Download the file to a byte[]
with the getBytes()
method. This is the
easiest way to download a file, but it 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. To protect against memory issues, getBytes()
takes a maximum amount of bytes to download. Set the maximum size to something
you know your app can handle, or use another download method.
StorageReference islandRef = storageRef.child("images/island.jpg"); final long ONE_MEGABYTE = 1024 * 1024; islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Data for "images/island.jpg" is returns, use this as needed } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
Download to a local file
The getFile()
method downloads a file directly to a local device. Use this if
your users want to have access to the file while offline or to share the file in a
different app. getFile()
returns a DownloadTask
which you can use to manage
your download and monitor the status of the download.
islandRef = storageRef.child("images/island.jpg"); File localFile = File.createTempFile("images", "jpg"); islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { // Local temp file has been created } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
If you want to actively manage your download, see Manage Downloads for more information.
Download Data via URL
If you already have download infrastructure based around URLs, or just want
a URL to share, you can get the download URL for a file by calling the
getDownload()
method on a storage reference.
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
Handle Activity Lifecycle Changes
Downloads continue in the background even after activity lifecycle changes (such as presenting a dialog or rotating the screen). Any listeners you had attached will also remain attached. This could cause unexpected results if they get called after the activity is stopped.
You can solve this problem by subscribing your listeners with an activity scope
to automatically unregister them when the activity stops. Then, use the
getActiveDownloadTasks
method when the activity restarts to obtain download
tasks that are still running or recently completed.
The example below demonstrates this and also shows how to persist the storage reference path used.
StorageReference mStorageRef; //mStorageRef was previously used to transfer data. @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // If there's a download in progress, save the reference so you can query it later if (mStorageRef != null) { outState.putString("reference", mStorageRef.toString()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // If there was a download in progress, get its reference and create a new StorageReference final String stringRef = savedInstanceState.getString("reference"); if (stringRef == null) { return; } mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef); // Find all DownloadTasks under this StorageReference (in this example, there should be one) Listtasks = mStorageRef.getActiveDownloadTasks(); if (tasks.size() > 0) { // Get the task monitoring the download DownloadTask task = tasks.get(0); // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this, new OnSuccessListener () { @Override public void onSuccess(DownloadTask.TaskSnapshot state) { handleSuccess(state); //call a user defined function to handle the event. } }); } }
Handle Errors
There are a number of reasons why errors may occur on download, including the file not existing, or the user not having permission to access the desired file. More information on errors can be found in the Handle Errors section of the docs.
Full Example
A full example of a download with error handling is shown below:
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Use the bytes to display the image } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
You can also get and update metadata for files that are stored in Firebase Storage.