I have been struggling to find a way which will save logcat to file using Scoped Access framework. I have gone through the below link SAF link, but no luck.
Till Android 10, I was using "requestLegacyExternalStorage" to make it work with the below code.
public void createLogs() {
if (isExternalStorageWritable()) {
File appDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/c4cex");
File logDirectory = new File(appDirectory + "/logs");
File logFile = new File(logDirectory, "c4cex.txt");
//File preLogonLogFile = new File(logDirectory, "preLogonLogs.txt");
// create app folder
if (!appDirectory.exists()) {
appDirectory.mkdir();
}
// create log folder
if (!logDirectory.exists()) {
logDirectory.mkdir();
}
try {
// Process process = Runtime.getRuntime().exec("logcat -f " + logFile);
Process process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 2048 -n 4"); // -r amount of bits to write, -n amount of logs to rotate
//preLogonLogFile.createNewFile();
//Process process1 = Runtime.getRuntime().exec("logcat -f " + preLogonLogFile + " -r 2048 -n 5"); // -r amount of bits to write, -n amount of logs to rotate
} catch (IOException e) {
e.printStackTrace();
}
}
}
After getting the logs in a file , I want to share it through email and hence SAF approach will only work as per the Android documents.