简体   繁体   English

无法关闭位于“for 循环”内的 addonsucesslistener() 内的进度对话框

[英]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.

相关问题 当字符串类型的列表中有逗号时,无法在 bigquery 中取消嵌套 - Unable to unnest in bigquery when there is comma inside list which is of type string Boto 无法访问具有正确 IAM 角色的 ECS 容器内的存储桶(但 Boto3 可以) - Boto is unable to access bucket inside ECS container which have correct IAM roles (but Boto3 can) 在 cloudformation 中的 yaml 中的 json 中使用 Fn::ImportValue - use Fn::ImportValue inside a json which is inside a yaml in cloudformation 为什么 setState 在嵌套在 useEffect 中的异步 function 中不起作用? - Why setState don't work inside of an async function which is nested inside of an useEffect? 为什么 python 代码无法在 EKS pod 容器内运行? - Why python code is unable to run inside a EKS pod container? 无法使用图形在管理单元内创建新组 API - Unable to create new group inside Administrative Unit using Graph API 访问存储在 firebase 实时数据库中的数据 - accessing data which is stored inside firebase real time database 如何从 goroutine 内部终止无限循环? - How do I terminate an infinite loop from inside of a goroutine? Firebase 在 useEffect 中的 onSnapshot; 为什么会无限循环? - Firebase's onSnapshot inside a useEffect; why does this infinitely loop? 如何在 for 循环中调用表格 function - How to call tabular function inside the for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM