我正在使用for循环从arraylist上传一系列文件
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFilePaths();
for (int i = 0; i < mFileURIs.size(); i++) {
Uri uri = mFileURIs.get(i);
uploadPDF(uri, mFileNAMES.get(i));
}
initiatePayment();
所有文件上传后如何执行其他功能?由于其他函数具有在文件上传时填充的变量,因此mFileURLs会在文件上传后存储文件的URL。
private void uploadPDF(Uri uri, String pdfName) {
mFileURLS = new ArrayList<>();
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference reference = storage.getReference();
StorageReference pdfFolder = reference.child(user.getUid());
StorageReference pdfRef = pdfFolder.child(pdfName);
pdfRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
pdfRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
Log.e("DD", "onSuccess: " + url);
mFileURLS.add(url);
counter++;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
Toast.makeText(getApplicationContext(), "UPLOADED SUCCESSFULLY", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("FirebaseActivity", "On Failure: " + e.getMessage());
Toast.makeText(getApplicationContext(), "FAILED TO UPLOAD", Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");
}
});
}
类似于此问题-Firebase Storage - Wait till all upload tasks are completed before executing function
但是在android中。谢谢!