androidfile-iodirectorydelete-directory

How to delete a whole folder and content?


I want the users of my application to be able to delete the DCIM folder (which is located on the SD card and contains subfolders).

Is this possible, if so how?


Solution

  • Let me tell you first thing you cannot delete the DCIM folder because it is a system folder. As you delete it manually on phone it will delete the contents of that folder, but not the DCIM folder. You can delete its contents by using the method below:

    Updated as per comments

    File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); 
    if (dir.isDirectory()) 
    {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++)
        {
           new File(dir, children[i]).delete();
        }
    }