I am running into a issue when trying to save the logcat to a file . Below is the code:
public void createLogs() {
if (isExternalStorageWritable()) {
File appDirectory;
if(Build.VERSION.SDK_INT >= 30) {
appDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/myTest");
}else{
appDirectory = new File(Environment.getExternalStorageDirectory() + “/myTest");
}
File logDirectory = new File(appDirectory + "/logs");
File logFile = new File(logDirectory, “mytest.txt");
if (!appDirectory.exists()) {
appDirectory.mkdir();
}
if (!logDirectory.exists()) {
logDirectory.mkdir();
}
try {
//Process process = Runtime.getRuntime().exec("logcat -c");
Process process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 2048 -n 4");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
It creates the folder myTest-->logs--->mytest.txt in this folder structure. It is creating .txt file but when manually delete .txt is not creating again. Please suggest.