万一哪天要用找不到
使用 Runtime 获取日志并保存至 download 目录。
try {
final String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator;
ArrayList<String> commandLine = new ArrayList<>();
commandLine.add("logcat");
//使用该参数可以让logcat获取日志完毕后终止进程
commandLine.add("-d");
commandLine.add("-v");
commandLine.add("time");
//如果使用commandLine.add(">");是不会写入文件,必须使用-f的方式
commandLine.add("-f");
commandLine.add(path + "log_" + System.currentTimeMillis() + ".txt");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);
String line = bufferedReader.readLine();
} catch (Exception e) {
e.printStackTrace();
}