“旋转轮”从 SD 卡删除文件夹时的进度对话框

发布于 2024-11-27 01:01:10 字数 876 浏览 2 评论 0原文

我想在从 SD 卡删除文件夹时用旋转轮显示简单的进度对话框。我有以下一段代码:

  ProgressDialog dialog = ProgressDialog.show(this, "",
            "Please wait for few seconds...", true);

private void deleteCache() {

    File f = new File(Environment.getExternalStorageDirectory()
            .getAbsoluteFile() + Constants.DATA_DIR);
    deleteDirectory(f);
    dialog.dismiss();
}

 private void deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

应该在 deleteDirectory(f); 之前显示对话框,并在结束后将其关闭。但我从未看到任何对话框,尽管该文件夹已被删除。

I want to display simple progress dialog with rotating wheel, while deleting folder from SD card. I have a following piece of code:

  ProgressDialog dialog = ProgressDialog.show(this, "",
            "Please wait for few seconds...", true);

private void deleteCache() {

    File f = new File(Environment.getExternalStorageDirectory()
            .getAbsoluteFile() + Constants.DATA_DIR);
    deleteDirectory(f);
    dialog.dismiss();
}

 private void deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

Which is supposed to show dialog before deleteDirectory(f); and dissmis it after it ends. But I never see any dialog, event though the folder is being deleted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那支青花 2024-12-04 01:01:10

这个答案也遍布 StakcOverflow。使用 AsyncTask 它将在不同的线程上运行并具有三个阶段。一个是您将加载轮子的前一个,一个是完成后您将关闭它的后一个……然后是实际工作的背景。

This answer is also all over StakcOverflow. Use AsyncTask which will run on a different thread and has three stages... One pre which you will load the wheel in and the post which you will dismiss it when done... And then the background which is the actual work.

年少掌心 2024-12-04 01:01:10
private void deleteCache() {
  ProgressDialog dialog = ProgressDialog.show(this, "",
    "Please wait for few seconds...", true);

  Runnable myRun = new Runnable() {
    public void run() {
      File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
      deleteDirectory(f);

      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          dialog.dismiss();
        }
      });
    }
  }
}

private void deleteDirectory(File path) {
  if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
      if (files[i].isDirectory()) {
        deleteDirectory(files[i]);
      } else {
        files[i].delete();
      }
    }
  }

  return (path.delete());
}
private void deleteCache() {
  ProgressDialog dialog = ProgressDialog.show(this, "",
    "Please wait for few seconds...", true);

  Runnable myRun = new Runnable() {
    public void run() {
      File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
      deleteDirectory(f);

      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          dialog.dismiss();
        }
      });
    }
  }
}

private void deleteDirectory(File path) {
  if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
      if (files[i].isDirectory()) {
        deleteDirectory(files[i]);
      } else {
        files[i].delete();
      }
    }
  }

  return (path.delete());
}
土豪 2024-12-04 01:01:10

像这样修改你的代码,

 ProgressDialog dialog = ProgressDialog.show(this, "",
        "Please wait for few seconds...", true);
        new Thread(new Runnable() {
        public void run() {
              File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
deleteDirectory(f);
   private void deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}


            handler.sendEmptyMessage(0);

        }
    }).start();

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {



                progressDialog.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    };

我不确定为什么会发生这种情况。由于您的代码在单个线程中完成所有操作,因此进度对话框不会很快显示。因此,尝试在单独的线程中处理其他事情可以解决这个问题。

Modify your code like this,

 ProgressDialog dialog = ProgressDialog.show(this, "",
        "Please wait for few seconds...", true);
        new Thread(new Runnable() {
        public void run() {
              File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
deleteDirectory(f);
   private void deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}


            handler.sendEmptyMessage(0);

        }
    }).start();

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {



                progressDialog.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    };

I am not sure why this happens. Since your code does everything in a single thread, progressdialog will not show up quickly. So instead trying to handle other things in a separate thread handles this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文