我想在用戶想要收集日誌時將Android logcat轉儲到文件中。通過adb工具,我們可以使用adb logcat -f filename
將日誌重定向到文件,但是如何以編程方式執行此操作?將android logcat數據寫入文件
回答
這是讀取日誌的example。
您可以將其更改爲寫入文件而不是寫入TextView
。
需要得到許可在AndroidManifest
:
<uses-permission android:name="android.permission.READ_LOGS" />
代碼:
public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}
logcat的可以直接寫入文件:
public static void saveLogcatToFile(Context context) {
String fileName = "logcat_"+System.currentTimeMillis()+".txt";
File outputFile = new File(context.getExternalCacheDir(),fileName);
@SuppressWarnings("unused")
Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}
上的logcat
更多的信息:看到http://developer.android.com/tools/debugging/debugging-log.html
由於logcat -f不會自動退出,因此該命令似乎失速。以任何方式將日誌寫入文件並自動退出類似於logcat -d的進程? – Phillip 2014-01-08 06:39:27
嘗試把&放到最後使其成爲背景。 – ShihabSoft 2015-06-01 10:49:40
logcat -df
或者你可以試試這個瓦里安
try { final File path = new File( Environment.getExternalStorageDirectory(), "DBO_logs5"); if (!path.exists()) { path.mkdir(); } Runtime.getRuntime().exec( "logcat -d -f " + path + File.separator + "dbo_logcat" + ".txt"); } catch (IOException e) { e.printStackTrace(); }
public static void writeLogToFile(Context context) {
String fileName = "logcat.txt";
File file= new File(context.getExternalCacheDir(),fileName);
if(!file.exists())
file.createNewFile();
String command = "logcat -f "+file.getAbsolutePath();
Runtime.getRuntime().exec(command);
}
以上方法將所有日誌寫入到文件中。也請在下面添加權限清單文件
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 1. 將數據寫入文件
- 2. 將數據寫入文件
- 3. 將數據寫入文件
- 4. 在JNI函數中將jstring寫入logcat
- 5. 瞭解logcat輸出時寫入文件
- 6. 將數據寫入文本文件
- 7. 將dumpstate寫入文件android
- 8. 將數據從文件寫入數組
- 9. 在Android中將日誌數據寫入/讀入文件
- 10. 如何將.txt文件數據加載到logcat android工具中?
- 11. Android在文件中寫入整個logcat消息
- 12. 從ListView將數據寫入Android上的文本文件
- 13. 將數據寫入Android中的URL上的文本文件
- 14. Android Firebase - 將數據寫入數據庫
- 15. 如何停止在dos提示符下將android logcat寫入文件
- 16. 將RTP流數據寫入文件
- 17. 將數據寫入CSV文件
- 18. 如何將數據寫入.SLK文件
- 19. c - 將數據寫入wav文件
- 20. Java Servlet - 將數據寫入文件
- 21. NODEjs - 將數據從mysql寫入文件
- 22. 將數據集寫入xml文件
- 23. 如何將數據寫入xml文件
- 24. C#將數據寫入CSV文件
- 25. 將數據從程序寫入文件
- 26. 將數據寫入.cof文件
- 27. 無法將數據寫入文件
- 28. TCL:無法將數據寫入文件
- 29. 將實時數據寫入文件
- 30. 將數據寫入php文件
http://stackoverflow.com/a/8417757/1012284 – 2012-07-03 09:22:13