0

I'm trying to delete all files that are exists in a known path.

I've used the next function to do so -

public void deleteAllImages(){
     Log.d(TAG, "ENTERD DELETED ALL IMAGES ");
    String path = "/data/data/yourapp/app_data/imageDir/";
    File directory = new File(path);


    if (directory.isDirectory()){
         Log.d(TAG, "ENTERED IF ");
         for (File child : directory.listFiles()){
             Log.d(TAG, "ENTERED FOR "+ child);
             child.delete();
         }
    }
}

But it seem that it never getting into the if statement - guess it mean that it doesn't treat directory as one. So what am I doing wrong here?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
4this
  • 759
  • 4
  • 13
  • 27

3 Answers3

2

Most likely the path is not correct. The isDirectory() will return false in the following cases:

  • The path points to file (obviously), and not to directory.

  • The path is invalid (i.e. there is no such file/directory exists).

  • There is not enough permissions granted to your application to determine whether path points to directory.

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
0

Try this :

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

Check this : Delete files from folder

Hope this helps.

Community
  • 1
  • 1
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

try this...

        String path = "/data/data/yourapp/app_data/imageDir/"; 
        File file = new File(path);
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            if(files != null && files.length > 0) {
              for (File file2 : files) {
                file2.delete();
              }
            }
        }
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43