读取Log日志并打印到sdcard

 1 @SuppressLint("SimpleDateFormat")
 2     private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 3 
 8     public static void LogWrite() {
 9         File file = new File(Constants.APP_PATH_LOG);
10         if (!file.exists() || !file.isDirectory()) {
11             file.mkdirs();
12         }
13         FileWriter fileWriter;
14         StringBuilder log;
15         try {
16             Process process = Runtime.getRuntime().exec("logcat -d -v time");
17             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
18 
19             log = new StringBuilder();
20             String line;
21             while ((line = bufferedReader.readLine()) != null) {
22                 log.append(line);
23                 log.append("
");
24             }
25             fileWriter = new FileWriter(Environment.getExternalStorageDirectory() + "/" + “log-” + sdf.format(new Date()).substring(0, 10) + ".txt", true);
26             fileWriter.write(log.toString());29             fileWriter.flush();
30             fileWriter.close();
31             Runtime.getRuntime().exec("logcat -c");32         } catch (IOException e) {
33 
34         } catch (OutOfMemoryError e) {
35             
36         }
37        
67     }
原文地址:https://www.cnblogs.com/slz-bky/p/5881365.html