2015-11-02 122 views
0

我想通過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程序。這裏有什麼竅門?

此外,你好人可以幫我解決這個問題嗎?

+0

如果你從命令行運行它,你可以在PC上執行它。但是如果你從你的程序運行它,你可以在你的Android設備上執行它。 轉到你的cur命令行並執行'adb shell'。現在你在你的Android外殼。從這裏你可以執行與你的程序相同的命令。 –

+0

恩,謝謝你的回答。如果我運行一個命令「logcat -v time> /sdcard/logcat.txt」,它似乎很好。但是,我想在程序中運行它,而不是在命令行中運行。 – GAVD

+0

'> D:\\ Test_logcat.txt'在第一種情況下由cmd解釋,由第二種情況下的android shell解釋,在這種情況下,它不能創建此文件。嘗試讀取輸出並直接從java寫入文件 – njzk2

回答

0

嘗試使用logcat -v time -d > /mnt/local/log.txt

注:文件夾,您可以存儲的數據是設備依賴,你可能需要更改文件路徑。