【问题标题】:Write output of android shell command to a file将android shell命令的输出写入文件
【发布时间】:2015-03-11 11:19:29
【问题描述】:
我正在尝试将 atrace 的输出写入 sdcard。我在用
getRuntime().exec()("atrace gfx > /storage/sdcard0/trace.txt")
在应用程序中。应用程序被签名为系统应用程序,该命令在终端上运行正常。但是从应用程序运行时没有创建文件。
【问题讨论】:
标签:
android
android-manifest
file-handling
systrace
【解决方案1】:
我通过从输入流中读取数据并将其写入文件来解决它
try {
Process p = Runtime.getRuntime().exec("atrace -t 5 gfx");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
File myFile = new File("/storage/sdcard0/trac.txt");
FileOutputStream f = null;
try {
f = new FileOutputStream(myFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(f);
while (line != null) {
pw.println(line);
//Toast.makeText(getBaseContext(), line, Toast.LENGTH_LONG).show();
line = reader.readLine();
}
pw.flush();
pw.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}