How to delete directory from internal storage on Android?

0

I have two functions to save files in the internal memory of my application, one for data in Json and another for Images.

With each new application update, I will need to remove these directories so that the new files are saved instead of the old ones.

Follow the code I'm using to create and save the data.

public static Boolean SaveJsonData(Context context, String dir, String filename, String jsondata) {
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(new File(context.getDir(dir, Context.MODE_PRIVATE), filename));
        fileOutputStream.write(jsondata.getBytes(Charset.forName("UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
                return true;
            } else { return false; }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

Files are saved in the directories:

/data/data/br.com.meuapp.app/app_json

/data/data/br.com.meuapp.app/app_icones

How do I remove these two directories with everything inside it?

    
asked by anonymous 18.05.2018 / 06:26

2 answers

0

I found an answer on the stack at the following link: link

In this response it has the following function:

   public int removeDirectory(final File folder) {
        if(folder.isDirectory() == true) {
            File[] folderContents = folder.listFiles();
            int deletedFiles = 0;
            if(folderContents.length == 0) {
                if(folder.delete()) {
                    deletedFiles++;
                    return deletedFiles;
                }
            } else if(folderContents.length > 0) {
                do {
                    File lastFolder = folder;
                    File[] lastFolderContents = lastFolder.listFiles();
                    //This while loop finds the deepest path that does not contain any other folders
                    do {
                        for(File file : lastFolderContents) {
                            if(file.isDirectory()) {
                                lastFolder = file;
                                lastFolderContents = file.listFiles();
                                break;
                            } else {
                                if(file.delete()) {
                                    deletedFiles++;
                                } else {
                                    break;
                                }
                            }//End if(file.isDirectory())
                        }//End for(File file : folderContents)
                    } while(lastFolder.delete() == false);
                    deletedFiles++;
                    if(folder.exists() == false) { return deletedFiles; }
                } while(folder.exists());
            }
        }
        else { return -1; }
         return 0;
    }

I found this function to be perfect, because it not only excludes any directory, but also returns the number of records deleted.

And to get the directory I need to delete I'm picking up the following line:

File Pathdir = context.getDir(dir, Context.MODE_PRIVATE);

And by the end my function looked like this:

public static int DeleteInternalFiles(Context context, String dir){
    File Pathdir = context.getDir(dir, Context.MODE_PRIVATE);
    return removeDirectory(Pathdir);
}
    
18.05.2018 / 09:20
0

Do this:

String path = "caminho da pasta/arquivo, etc";

boolean p = new File(path).delete();

if(p){
     Toast.makeText(view.getContext(), "Deletado com sucesso!", Toast.LENGTH_SHORT).show();
}else{
     Toast.makeText(view.getContext(), "Erro inesperado!", Toast.LENGTH_SHORT).show();
}
    
18.05.2018 / 07:53