2010-07-29 105 views

回答

21

使用logcat -f <filename>爲了將其轉儲到文件系統中的文件。
確保您使用的文件名位於SD卡中,即以/sdcard/...開頭。

此外,爲了參數傳遞給logcat程序,你應該通過exec方法字符串數組(而不是一個字符串):

String[] cmd = new String[] { "logcat", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" }; 

最後,如果一切都失敗了,使用全logcat的路徑:/system/bin/logcat而不是logcat

+1

thanx的reply..I嘗試相同的,但我得到空文件 – 2010-07-29 06:15:52

+0

請注意,它可能需要一些時間日誌文件顯示的內容,因爲它可能會緩衝寫入文件(續寫一次只有幾個KB) – adamk 2010-07-29 07:10:14

+2

另一件重要的事情 - 確保你的應用具有WRITE_EXTERNAL_STORAGE和READ_LOGS權限 - 否則它將不允許它讀取日誌並將它們寫入SD卡。 – adamk 2010-07-29 07:12:16

0

嘗試在字符串中的每個元素之後使用空格。否則,它會認爲是沒有空格的單行命令,什麼都不做。

+0

嗨, 我試過了,它給我空文件...這是我試過的掃描笏, 文件filename = new File(「/ sdcard/logfile.log」); filename.createNewFile(); String [] cmd = new String [] {「logcat」,「-v」,「time」,「ActivityManager:W」,「myApp:D」,「*:*」,「\」「+ filename.getAbsolutePath ()+ 「\」「}; Runtime.getRuntime()。exec(cmd); – 2010-07-30 04:01:28

23

這是我的工作版本:

try { 
    File filename = new File(Environment.getExternalStorageDirectory()+"/logfile.log"); 
    filename.createNewFile(); 
    String cmd = "logcat -d -f "+filename.getAbsolutePath(); 
    Runtime.getRuntime().exec(cmd); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

找到你的日誌文件/sdcard/logfile.log

+6

我仍然得到一個空文件logFile.log。我已經添加了WRITE_EXTERNAL_STORAGE和READ_LOGS權限 – kmalmur 2011-11-23 09:14:19

+0

'-d'導致只寫入當前緩衝區。然後Logcat停止寫入任何附加數據。看到熊的答案,以獲得連續寫作。 – battlepope 2016-05-30 10:45:35

4
/** 
    * Launches a logcat process. 
    * To stop the logcat preserve the use: 
    * process.destroy(); 
    * in the onBackPressed() 
    * 
    * @param filename destination of logcat output 
    * @return Process logcaat is running on 
    */ 

    public Process launchLogcat(String filename) { 
     Process process = null; 
     String cmd = "logcat -f " + filename + "\n"; 
     try { 
      process = Runtime.getRuntime().exec(cmd); 
     } catch (IOException e) { 
      process = null; 
     } 
     return process; 
    } 
+0

謝謝。但我應該在哪裏放這個代碼?我將如何開始這個過程? – Pontios 2016-01-26 11:48:18

+0

將上面的代碼放在* main_activity *類中。然後創建一個**私人進程進程**,並在* onCreate *方法中像這樣進行混淆:** process = launchLogcat(「sdcard/log.file」); **完成! – Pontios 2016-01-26 12:07:43

0

如果您收到一個空的日誌文件,嘗試文件的擴展名從.log改變到.txt

0
public static void saveLogInSDCard(Context context){ 
    String filename = Environment.getExternalStorageDirectory() + File.separator + "project_app.log"; 
    String command = "logcat -d *:V"; 

    try{ 
     Process process = Runtime.getRuntime().exec(command); 

     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line = null; 
     try{ 
      File file = new File(filename); 
      file.createNewFile(); 
      FileWriter writer = new FileWriter(file); 
      while((line = in.readLine()) != null){ 
       writer.write(line + "\n"); 
      } 
      writer.flush(); 
      writer.close(); 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 
相關問題