在没有连接计算机的情况下,Android设备本地转储崩溃日志的方法是使用logcat命令将日志输出到设备上的文件中。以下是代码示例:
File outputFile = new File(Environment.getExternalStorageDirectory() + "/myapplog.txt"); try { Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); StringBuilder log=new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { log.append(line); log.append("\n"); } FileOutputStream fileOutputStream = new FileOutputStream(outputFile); fileOutputStream.write(log.toString().getBytes()); bufferedReader.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); }
注:此方法将日志输出到SD卡上的myapplog.txt文件中,你需要添加读写SD卡的权限。