2

作为错误报告,我想将设备和/或应用程序日志发送到电子邮件帐户。为此,我在我的视图中准备了一个按钮。有人可以帮我解决这个问题,我该如何检索日志文件?

谢谢

4

2 回答 2

3
            String separator = System.getProperty("line.separator");
            try {
                Process process = Runtime.getRuntime().exec("logcat -d");
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));

                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    log.append(line);
                    log.append(separator);
                }
            } catch (Exception e) {

            }
于 2013-01-10T20:29:20.743 回答
2

这是一个稍微改进的版本,改编自 Ramesh(谢谢)。主要区别是自己做缓冲。它很快。

  private void extractLogToFile()
  {
    // Make file name.
    String fullName = ...;

    // Extract to file.
    File file = new File (fullName);
    InputStreamReader reader = null;
    FileWriter writer = null;
    try
    {
      // get input stream
      String cmd = "logcat -d -v time";
      Process process = Runtime.getRuntime().exec(cmd);
      reader = new InputStreamReader (process.getInputStream());

      // write output stream
      writer = new FileWriter (file);
      char[] buffer = new char[10000];
      do 
      {
        int n = reader.read (buffer, 0, buffer.length);
        if (n == -1)
            break;
        writer.write (buffer, 0, n);
      } while (true);

      reader.close();
      writer.close();
    }
    catch (IOException e)
    {
      if (writer != null)
          try {
            writer.close();
        } catch (IOException e1) {
        }
      if (reader != null)
          try {
            reader.close();
        } catch (IOException e1) {
        }
      e.printStackTrace();
      return;
    }
  }
于 2013-11-08T00:27:29.007 回答