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()); } });
However, this method is now deprecated.
Updated Method
The updated method involves using the StorageReference.getDownloadUrl() method. To use this method, you need to:
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 } }); } });
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!