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?
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??– Samanta Silva
I figured it out, I’ll post the answer.
– Samanta Silva