swiftbegineer
swiftbegineer

Reputation: 331

How to download multiple files of firebase Storage Android?

I am downloading files from firebase storage But I can only download one by one Can I download multiple files at once? Is it the best way to repeat the same code?

FirebaseStorage storage = FirebaseStorage.getInstance();

StorageReference storageRef = storage.getReference();
StorageReference islandRef = storageRef.child(filename);

final String saveFilename = filename;
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/down/");

// If no folders
if (!dir.exists()) {
    dir.mkdirs();
}

final File localFile = new File(dir,saveFilename);


islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
        

    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {

    }
});

Upvotes: 0

Views: 1431

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317828

Simply call putFile once for each file you want to download. You will need a different reference and local file for each call.

FileDownloadTask task1 = storageRef1.getFile(localFile1);
FileDownloadTask task2 = storageRef2.getFile(localFile3);
FileDownloadTask task3 = storageRef3.getFile(localFile3);

You can then wait for all them to complete with Tasks.whenAll():

Tasks.whenAll(task1, task2, task3)
.addOnSuccessListener(new OnSuccessListener<List<Task<*>>() {
    @Override
    public void onSuccess(Task task) {
        

    }
})
.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {

    }
});

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

There is no specific API to download multiple files. You'll just have to download them by calling the same API for each file.

Upvotes: 1

Related Questions