2011-06-29 30 views
0

我想在我的java代碼中執行「adb logcat -d time> pathoffile \ log.txt &」。如何執行「adb logcat -d time> pathoffile log.txt&」程序設計

我想在我的設備中創建我的log.txt。

我寫了這段代碼來做到這一點。

ArrayList<String> commandLine = new ArrayList<String>(); 
     commandLine.add("logcat"); 
     commandLine.add("-d"); 
     commandLine.add("time"); 
     commandLine.add(">"); 
     commandLine.add(getApplicationContext().getFilesDir() 
     //  + "/log.txt"); //already created the file at specified location. 
     //commandLine.add("&"); 
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0])); 

上面的代碼不扔任何錯誤,但我的文件(log.txt的)不與日誌報表更新..

請幫我在這如何做到這一點,PL。建議如果有任何替代做到這一點.. 謝謝。

+0

你是如何創建文件的?也許這是一個權限問題? – Mark

+0

我創建了這樣的文件:File logf = new File(getApplicationContext()。getFilesDir() \t \t \t \t \t +「/log.txt」); \t \t \t logf.createNewFile();我可以通過導航到該文件目錄來查看log.txt文件。所以我認爲它不是一個許可問題。如果它將拋出IO異常。 – brig

回答

4

嘗試-f選項,而不是:

adb logcat -d time -f /mnt/sdcard/log.txt 

注意>是外殼的IO重定向功能。與exec()功能配合使用時無效。

+0

感謝inazaruk,現在它的工作,我們還需要將READ_LOGS權限添加到清單文件中。 – brig