通过java通过wifi从adb获取logcat
问题描述:
我想通过java通过wifi从android设备到PC获取logcat。程序将按时间获取logcat,并在单击按钮时停止。通过java通过wifi从adb获取logcat
所以,我可以连接到Android通过WiFi和得到的logcat:
adb -s [ip_address]:5555 logcat -v time > D:\logcat.txt
此命令运行以及在cmd中。当我按下时,它会停止。Ctrl + C。但是当我在java代码中运行这个命令时。
Process proc;
String command = "adb -s [ip_address]:5555 logcat -v time > D:\\Test_logcat.txt";
Runtime rt = Runtime.getRuntime();
try {
proc = rt.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
String line = null;
while ((line = stdInput.readLine()) != null)
System.out.println(line);
while ((line = stdError.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
它提出了这样的错误:
/system/bin/sh: can't create D:\Test_logcat.txt: Read-only file system
确定。我搜索谷歌并得到这个问题的答案。 Here,他们解释说:
In your command line example, you are running the ADB command from the Android terminal with/as current directory. So Android tries to write the output to /logcat.txt, which fails.
但是,我不明白为什么这个命令可以在cmd中正常运行,而不是在java程序。这里有什么窍门?
此外,你好人可以帮我解决这个问题吗?
答
尝试使用logcat -v time -d > /mnt/local/log.txt
注:文件夹,您可以存储的数据是设备依赖,你可能需要更改文件路径。
如果你从命令行运行它,你可以在PC上执行它。但是如果你从你的程序运行它,你可以在你的Android设备上执行它。 转到你的cur命令行并执行'adb shell'。现在你在你的Android外壳。从这里你可以执行与你的程序相同的命令。 –
恩,谢谢你的回答。如果我运行一个命令“logcat -v time> /sdcard/logcat.txt”,它似乎很好。但是,我想在程序中运行它,而不是在命令行中运行。 – GAVD
'> D:\\ Test_logcat.txt'在第一种情况下由cmd解释,由第二种情况下的android shell解释,在这种情况下,它不能创建此文件。尝试读取输出并直接从java写入文件 – njzk2