无法关闭 "for loop" 内的 addonsucesslistener() 内的进度对话框
Unable to dismiss the progressdialog inside the addonsucesslistener() which is inside "for loop"
我正在选择多个图像并将它们存储到 Firebase 存储中,然后在 ArrayList 中检索下载 URL 并将其添加到 Firebase 数据库中,我的代码工作正常但我不知道什么时候停止进度对话框,我必须关闭进度对话框,这样当进度条的任务完成时,进度对话框就会消失。
这是我的代码:
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);
}
在代码中的 addprogressListner 之后替换 mDialog.dismiss();
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);
}
当您调用 uploadScreenshot()
时,您要做的第一件事就是显示 ProgressDialog。当您将文件上传到 Firebase 存储时,您得到的响应可能是成功也可能是失败,绝不会两者兼而有之。这意味着您应该关闭 onSuccess() 和 onFailure() 方法中的对话框。如果您已成功上传文件,则应关闭该对话框,如果失败也应该关闭该对话框。所以请添加以下代码行:
mDialog.dismiss();
在 onSuccess() 和 onFailure() 方法内部。
编辑:
如果您只想在上传所有文件后关闭对话框,那么您应该使用递归,正如我在以下 post:
的回答中所解释的那样
这意味着当列表大小变为零时,您可以关闭该对话框。
我正在选择多个图像并将它们存储到 Firebase 存储中,然后在 ArrayList 中检索下载 URL 并将其添加到 Firebase 数据库中,我的代码工作正常但我不知道什么时候停止进度对话框,我必须关闭进度对话框,这样当进度条的任务完成时,进度对话框就会消失。
这是我的代码:
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);
}
在代码中的 addprogressListner 之后替换 mDialog.dismiss();
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);
}
当您调用 uploadScreenshot()
时,您要做的第一件事就是显示 ProgressDialog。当您将文件上传到 Firebase 存储时,您得到的响应可能是成功也可能是失败,绝不会两者兼而有之。这意味着您应该关闭 onSuccess() 和 onFailure() 方法中的对话框。如果您已成功上传文件,则应关闭该对话框,如果失败也应该关闭该对话框。所以请添加以下代码行:
mDialog.dismiss();
在 onSuccess() 和 onFailure() 方法内部。
编辑:
如果您只想在上传所有文件后关闭对话框,那么您应该使用递归,正如我在以下 post:
的回答中所解释的那样这意味着当列表大小变为零时,您可以关闭该对话框。