I have an array on URI files and i upload them on firebase storage with a for loop. Following the documentation, i can get the download url implementing continueWithTask
and addOnCompleteListener
methods on UploadTask
object, so i try to save all the url inside an array. My problem is that the array that i use to save the url is empty but the images are uploaded on firestore Storage folder after the computation of the code probably because continueWithTask
and addOnCompleteListener
are asynchronous methods.
How can i solve it?
for (int i = 0; i < photos.size(); i++) {
mStorage=FirebaseStorage.getInstance().getReference().child("images/" + j);
mUploadTask=mStorage.putFile(photos.get(i));
Task<Uri>urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
return mStorage.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
String url = task.getResult().toString();
downloadUrl.add(url);
}
}
});
}
My array downloadUrl is empty after the request of the url