2

Possible Duplicate:
Delete a folder on SD card

In my app i saved all my data using internal storage i.e file. So at the first instance by using the ContextWrapper cw = new ContextWrapper(getApplicationContext()); class i get the directory path as m_AllPageDirectoryPath = cw.getDir("AllPageFolder", Context.MODE_PRIVATE); Inside this directory path i saved some file File as Page01, page02, Page03 and so on.

Again inside Page01 i saved some file like image01, image02...using the same concept m_PageDirectoryPath = cw.getDir("Page01", Context.MODE_PRIVATE); Now on delete of m_AllPageDirectoryPath i want to delete all the file associate with it. I tried using this code but it doesn't work.

File file = new File(m_AllPageDirectoryPath.getPath()); 
file.delete();
Community
  • 1
  • 1
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

1 Answers1

3

Your code only works if your directory is empty.

If your directory includes Files and Sub Directories, then you have to delete all files recursively..

Try this code,

// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}

(Actually you have to search on internet before asking like this questions)

user370305
  • 108,599
  • 23
  • 164
  • 151