我正在尝试以编程方式使用log.i()
读取用户输入的日志,如下所示,
try {
Log.i("logs", "inside log.java");
String filter = "logcat ";
filter += "-d ";
filter += "logs:I";
// String[] command = new String[] { "logcat", "-s" , "logs:"+filter
// };
Process process = Runtime.getRuntime().exec(filter);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(onCleared);
tv = (TextView) findViewById(R.id.logview);
tv.setText(log.toString());
} catch (IOException ex) {
Log.e("Logging", ex.toString());
}
// convert log to string
final String logString = new String(log.toString());
// create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
try {
// to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(logString);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但它只是显示黑屏。但是当我尝试在adb shell
中运行命令时,它可以工作。我的adb
代码是,
adb logcat -s logs:I
答案 0 :(得分:1)
在尝试以编程方式将文件记录到文本时,我遇到了类似的问题。我认为这里发生的事情是日志读取过程阻塞了UI(主)线程,导致黑屏。
尝试将整个日志读取过程放入ASyncTask中。这使它对我有用!
答案 1 :(得分:0)
您是否已将此添加到清单文件中?
<uses-permission android:name="android.permission.READ_LOGS" />