Home > Java > javaTutorial > How to Retrieve Download URLs from Firebase Storage: Deprecated vs. Updated Methods?

How to Retrieve Download URLs from Firebase Storage: Deprecated vs. Updated Methods?

Patricia Arquette
Release: 2024-12-23 05:19:39
Original
697 people have browsed it

How to Retrieve Download URLs from Firebase Storage: Deprecated vs. Updated Methods?

Getting Download URL from Firebase Storage

Firebase Storage offers a straightforward method to retrieve the download URL of an uploaded file. However, the syntax has changed over time.

Initial Method (Deprecated)

Older versions of Firebase Storage allowed you to retrieve the download URL directly from the UploadTask.TaskSnapshot object. You could use the following code:

uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>()
{
    @Override
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot)
    {
        Log.d("aaaaasessin",""+taskSnapshot.getTask().getResult());
    }
});
Copy after login

However, this method is now deprecated.

Updated Method

The updated method involves using the StorageReference.getDownloadUrl() method. To use this method, you need to:

  1. Add a success listener to the UploadTask:
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // Get a reference to the file
        StorageReference fileRef = taskSnapshot.getStorage();

        // Get a download URL
        fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                // The download URL is contained in the Uri object
            }
        });
    }
});
Copy after login
  1. Remember that getDownloadUrl() returns a Task object, not a URL. You need to use the addOnSuccessListener to retrieve the actual URL once the operation is complete.
  2. Note that the success listener will only be called if the device has connectivity to the Firebase Storage backend. If the device is offline, the listener may not be called.

List Download URLs

Since August 22, 2019, you can also use the StorageReference.list() method to get a list of download URLs for files stored in a directory. The list() method returns a ListResult object, which contains a list of StorageReference objects. You can then call getDownloadUrl() on each of these objects to retrieve the corresponding download URLs.

The above is the detailed content of How to Retrieve Download URLs from Firebase Storage: Deprecated vs. Updated Methods?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template