-1

I am working on an android tablet application. i have created a folder in SD card to store images . I want to delete this folder at some point how can i achieve this ? i am posting my code also.

// This is code for deletion of folder .


File file = new File(Environment.getExternalStorageDirectory()+"/Easy_Measurement_images");
             if(file.exists())
             {
             file.delete();
             }

This code not deleting folder from SD Card android.

user2306690
  • 39
  • 2
  • 4

2 Answers2

6

try this snippet.. it should do it..

private void recursiveDelete(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory())
            for (File child : fileOrDirectory.listFiles())
                recursiveDelete(child);

        fileOrDirectory.delete();
    }
d3m0li5h3r
  • 1,957
  • 17
  • 33
0

Please Try with the below code.

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();
            }

call this method like below.

  deleteDir(new File(FOLDER)); here folder is your folder name..

it will delete if the folder has any files in it also.

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31