Android logcat:使用电子邮件从设备发送日志条目

Ada*_*tan 26 android logcat

脚本

我已经向几个朋友发布了Android应用的测试版.现在,我想修复测试期间出现的一些错误.

我已经设置了第三方崩溃报告实用程序,因此我可以轻松处理应用程序崩溃.但是,有一些错误的行为不会导致崩溃.在这些情况下,我想检查应用程序日志,看看出了什么问题.

有没有办法让应用程序通过电子邮件发送其logcat条目?

澄清

  • 有许多日志应用程序(android-log-collector,Log Viewer(logcat))可以检查和显示logcat条目.但是,自Android 4.1以来,这些应用无法访问其他应用的日志.
  • 我不介意在设备中占用大量空间 - 此功能仅适用于beta测试人员.
  • 解决方案应该没有root或任何其他特殊权限.

小智 26

在主活动的onDestroy中调用此方法.

 public void SendLogcatMail(){

    // save logcat in file
    File outputFile = new File(Environment.getExternalStorageDirectory(),
            "logcat.txt");
    try {
        Runtime.getRuntime().exec(
                "logcat -f " + outputFile.getAbsolutePath());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 //send file using email
 Intent emailIntent = new Intent(Intent.ACTION_SEND);
 // Set type to "email"
 emailIntent.setType("vnd.android.cursor.dir/email");
 String to[] = {"yourmail@gmail.com"};
 emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
 // the attachment
 emailIntent .putExtra(Intent.EXTRA_STREAM, outputFile.getAbsolutePath());
 // the mail subject
 emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
 startActivity(Intent.createChooser(emailIntent , "Send email..."));
 }
Run Code Online (Sandbox Code Playgroud)

  • 这可以通过 `emailIntent .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));` 解决。但是后来,附件是空的(或者 Gmail 应用程序告诉我的。) (2认同)