我正在嘗試將我的應用程序的日誌重定向到sdcard文件。但我沒有這樣做。我正在嘗試這樣的事情。如何將日誌輸出從logcat重定向到android設備上的SD卡?
String cmd= "logcat -v time ActivityManager:W myapp:D *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);
我也試過-f選項,但它也不工作。
我正在嘗試將我的應用程序的日誌重定向到sdcard文件。但我沒有這樣做。我正在嘗試這樣的事情。如何將日誌輸出從logcat重定向到android設備上的SD卡?
String cmd= "logcat -v time ActivityManager:W myapp:D *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);
我也試過-f選項,但它也不工作。
使用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
。
嘗試在字符串中的每個元素之後使用空格。否則,它會認爲是沒有空格的單行命令,什麼都不做。
嗨, 我試過了,它給我空文件...這是我試過的掃描笏, 文件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
這是我的工作版本:
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
我仍然得到一個空文件logFile.log。我已經添加了WRITE_EXTERNAL_STORAGE和READ_LOGS權限 – kmalmur 2011-11-23 09:14:19
'-d'導致只寫入當前緩衝區。然後Logcat停止寫入任何附加數據。看到熊的答案,以獲得連續寫作。 – battlepope 2016-05-30 10:45:35
/**
* 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;
}
如果您收到一個空的日誌文件,嘗試文件的擴展名從.log
改變到.txt
。
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();
}
}
thanx的reply..I嘗試相同的,但我得到空文件 – 2010-07-29 06:15:52
請注意,它可能需要一些時間日誌文件顯示的內容,因爲它可能會緩衝寫入文件(續寫一次只有幾個KB) – adamk 2010-07-29 07:10:14
另一件重要的事情 - 確保你的應用具有WRITE_EXTERNAL_STORAGE和READ_LOGS權限 - 否則它將不允許它讀取日誌並將它們寫入SD卡。 – adamk 2010-07-29 07:12:16