[英]Unable to dismiss the progressdialog inside the addonsucesslistener() which is inside "for loop"
I'm selecting multiple images and storing them into Firebase Storage, and retrieving the download URL inside an ArrayList and adding it into the Firebase Database, my code is working fine but I don't know when to stop the progress dialog, where I have to dismiss the progress dialog so when the task of the progress bar is completed than progress dialog disappear.我正在选择多个图像并将它们存储到 Firebase 存储中,并在 ArrayList 中检索下载 URL 并将其添加到 Firebase 数据库中,我的代码工作正常但我不知道何时停止进度对话框,我在哪里关闭进度对话框,这样当进度条的任务完成时,进度对话框就会消失。
Here is my code:这是我的代码:
private void uploadScreenshot() {
if (screenshotUri != null) {
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.setMessage("Uploading...");
mDialog.show();
String ssName = UUID.randomUUID().toString();
final StorageReference screenshotFolder = storageReference.child("screenshots/" + ssName);
for (upload_count = 0; upload_count < ScreenshotList.size(); upload_count++) {
Uri IndividualImage = Uri.parse(ScreenshotList.get(upload_count));
final StorageReference ScreenshotName = screenshotFolder.child(IndividualImage.getLastPathSegment());
ScreenshotName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ScreenshotName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = String.valueOf(uri);
storeLink(url);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mDialog.dismiss();
Toast.makeText(AppList.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
mDialog.setMessage("Uploaded " + progress + "%");
}
});
}
}
// mDialog.dismiss();
//Toast.makeText(AppList.this, "Screenshots Uploaded !!!", Toast.LENGTH_SHORT).show();
}
private void storeLink(String url) {
ssList.add(url);
newApp.setScreenshot(ssList);
}
When you call the uploadScreenshot()
the first thing you do is to display a ProgressDialog.当您调用uploadScreenshot()
时,您要做的第一件事就是显示 ProgressDialog。 When you upload a file to Firebase Storage, the response that you get can be either a success or a failure, never both.当您将文件上传到 Firebase 存储时,您得到的响应可能是成功也可能是失败,绝不会两者兼而有之。 This means that you should dismiss the dialog in both onSuccess() and onFailure() methods.这意味着您应该关闭 onSuccess() 和 onFailure() 方法中的对话框。 If you have successfully uploaded the file, the dialog should be dismissed, as it also should be if you get a failure.如果您已成功上传文件,则应关闭该对话框,如果失败也应该关闭该对话框。 So please add the following lines of code:所以请添加以下代码行:
mDialog.dismiss();
Inside onSuccess() and onFailure() methods. onSuccess() 和 onFailure() 方法内部。
Edit:编辑:
If you want to dismiss the dialog only after uploading all files, then you should use recursion as explained in my answer from the following post:如果您只想在上传所有文件后关闭对话框,那么您应该按照我在以下帖子中的回答中的说明使用递归:
This means that when the size of the list becomes zero, you can dismiss the dialog.这意味着当列表的大小变为零时,您可以关闭该对话框。
replace mDialog.dismiss();替换mDialog.dismiss(); after the addprogressListner in your code在代码中的 addprogressListner 之后
private void uploadScreenshot() { if (screenshotUri;= null) { final ProgressDialog mDialog = new ProgressDialog(this). mDialog.setMessage("Uploading..;"). mDialog;show(). String ssName = UUID.randomUUID();toString(). final StorageReference screenshotFolder = storageReference;child("screenshots/" + ssName); for (upload_count = 0. upload_count < ScreenshotList;size(). upload_count++) { Uri IndividualImage = Uri.parse(ScreenshotList;get(upload_count)). final StorageReference ScreenshotName = screenshotFolder.child(IndividualImage;getLastPathSegment()). ScreenshotName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { ScreenshotName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { String url = String;valueOf(uri); storeLink(url); } }). } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { mDialog;dismiss(). Toast.makeText(AppList,this. "" + e,getMessage(). Toast.LENGTH_SHORT);show(). } }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { @Override public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot;getTotalByteCount()). mDialog;setMessage("Uploaded " + progress + "%"); } }). mDialog;dismiss(). Toast.makeText(AppList,this, "Screenshots Uploaded..;". Toast;LENGTH_SHORT).show(); } } } private void storeLink(String url) { ssList.add(url); newApp.setScreenshot(ssList); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.