2

I have used the android internal storage to save a file for my application.

Ex

File rootFolder = context.getFilesDir();
File albumIdFolder = new File(rootFolder,getAlbumId());

Basicaly i want to delete the folder

So i tried with

 File rootFolder = context.getFilesDir();
 File albumIdFolder = new File(rootFolder,getAlbumId());
 albumIdFolder.delete()

but this not working not deleting folder

I readed this answares but not worked in my case please help me solve this issue i am not getting where i am going wong.

Delete file from internal storage

How to delete internal storage file in android?

Edit 2

Ex ^(folder hierarchy)

data
    user
        0
         packageName
                    files
                        155775346846131
                                       otherData

i want to delete 155775346846131 folder

Joker
  • 153
  • 14

2 Answers2

4

You can delete files with folder like as below,

void deleteFiles(Context context) {
    File rootFolder = context.getFilesDir();
    File fileDir = new File( rootFolder,getAlbumId());
    if (fileDir.exists()) {
        File[] listFiles = fileDir.listFiles();
        for (File listFile : listFiles) {
        if (!listFile.delete()) {
            System.err.println( "Unable to delete file: " + listFile );
        }
        }
    }
    rootFolder.delete();
}

Source : How to delete a whole folder and content?

Don't forgot to give Storage permission.

Madhav
  • 317
  • 2
  • 12
1

You can delete files and folders recursively like this:

public void deleteFolderRecursive(File fileOrDirectory) {

   if (fileOrDirectory.isDirectory()) {
   for (File child : fileOrDirectory.listFiles()) {
      deleteFolderRecursive(child);
   }
}

 fileOrDirectory.delete();
}
Sanjay Bhalani
  • 2,424
  • 18
  • 44