How to delete directory from internal storage on Android?

Asked

Viewed 1,029 times

0

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

With each new app update, I will need to remove these directories so that new files are saved in place of 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 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 them?

2 answers

0

Do so:

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();
}
  • It didn’t work for me, from the following mistake: 
Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead less... (Ctrl+F1) 
Your code should not reference the /sdcard path directly; instead use Environment.getExternalStorageDirectory().getPath(). Similarly, do not reference the /data/data/ path directly; it can vary in multi-user scenarios. Instead, use Context.getFilesDir().getPath(). More info: ........ My question is exactly how to take the path of the directory to put into a function, like this: Minhaclasse.Deletardiretorio("json"); You know??

  • I figured it out, I’ll post the answer.

0


I found an answer in the stack on the following link: https://stackoverflow.com/a/46086062/9441092

In this answer you have 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 perfect function, pq not only deletes any directory, but also returns the number of deleted records.

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

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

And in the end my job was like this:

public static int DeleteInternalFiles(Context context, String dir){
    File Pathdir = context.getDir(dir, Context.MODE_PRIVATE);
    return removeDirectory(Pathdir);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.