首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将android logcat数据写入文件

将android logcat数据写入文件
EN

Stack Overflow用户
提问于 2011-05-30 18:16:38
回答 4查看 56.7K关注 0票数 69

每当用户想要收集日志时,我想在一个文件中转储Android logcat。通过adb工具,我们可以使用adb logcat -f filename将日志重定向到一个文件,但是我如何以编程方式做到这一点?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-30 18:53:57

这是一个读取日志的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) {
    }
  }
}
票数 125
EN

Stack Overflow用户

发布于 2012-11-15 07:30:57

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

票数 40
EN

Stack Overflow用户

发布于 2015-08-03 23:56:30

或者你也可以试试这个变种

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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6175002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档