“Rotating wheel” progress dialog while deleting folder from SD card

后端 未结 3 1650
予麋鹿
予麋鹿 2020-12-21 11:27

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 =         


        
3条回答
  •  滥情空心
    2020-12-21 11:49

    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.

提交回复
热议问题