Deleting files in Android
Asked Answered
G

3

6

I have a directory that contains a lot of files. I want to delete the entire directory as well as all the files in it.

I want my code to wait until every File in that directory (including the directory itself) is deleted before the next command is executed.

How do i wait? My code is

public void wipeMemoryCard() 
    {
        File deleteMatchingFile = new File(Environment 
                .getExternalStorageDirectory().toString()); 
        try { 
            filenames = deleteMatchingFile.listFiles(); 
            if (filenames != null && filenames.length > 0) 
            { 
                content = true;
                for (File tempFile : filenames) 
                { 
                    if (tempFile.isDirectory()) 
                    { 
                        wipeDirectory(tempFile.toString()); 
                        tempFile.delete();

                    } 
                    else 
                    {                       
                        File file = new File(tempFile.getAbsolutePath()); 
                        file.delete(); 
                    } 
                } 
            } 
            else 
            {   

                deleteMatchingFile.delete(); 
                Toast("No files to Delete");
            } 
        } 

        catch (Exception e) 
        { 
           e.printStackTrace();
        }
        if(content == true)
        {
              if (filenames == null && filenames.length == 0) 
              {
                  Toast("Files Deleted");
              }
        }
    } 

    private static void wipeDirectory(String name) { 
        File directoryFile = new File(name); 
        File[] filenames = directoryFile.listFiles(); 
        if (filenames != null && filenames.length > 0) 
        { 
            for (File tempFile : filenames) 
            { 
                if (tempFile.isDirectory()) 
                { 
                    wipeDirectory(tempFile.toString()); 
                    tempFile.delete(); 
                }
                else 
                { 
                    File file = new File(tempFile.getAbsolutePath()); 
                    file.delete();  
                } 
            } 
        } else 
        { 
            directoryFile.delete(); 
        } 
    } 
Gratia answered 10/1, 2012 at 5:51 Comment(0)
W
13

You should not run this on the UI thread. If the file deletion takes too long, the system will pop up an "Application Not Responding" error. You can do this with an AsyncTask. The documentation shows a simple way to use this to pop up a "please wait" dialog, do the time-consuming work in the background, and then dismiss the dialog.

P.S. Your method name is kind of scary! :)

Waterproof answered 10/1, 2012 at 5:59 Comment(0)
F
1

You Should use Handler for this so when all files gets deleted it will send message to handler to the next task that you want to perform .

see this link for the handler.

Fishhook answered 10/1, 2012 at 5:57 Comment(0)
P
0
public static void DeleteRecursive(String filename) {
    File file = new File(filename);
    if (!file.exists())
        return;
    if (!file.isDirectory()) {
        file.delete();
        return;
    }

    String[] files = file.list();
    for (int i = 0; i < files.length; i++) {

        DeleteRecursive(filename + "/" + files[i]);
    }
    file.delete();
}
Pibgorn answered 10/1, 2012 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.