2012-01-25 84 views
2

我需要bugreport选项,您可以在adb中使用该选项转到我的应用中的sd文件。我找到Android; using exec("bugreport"),它解释了你不能在常规shell中运行bugreport,并且需要单独运行dumpstate,dumpsys和logcat以获得相同的结果。这很好,我明白,但我不能得到dumpstate或dumpsys写入文件。以下工作正常使用logcat -d -f编写logcat,但不适用于其他两个。我已经尝试过dumpstate -f,dumpstate -d -f和dumpstate>来使它工作,但是仍然没有向文件写任何东西。有没有什么我失踪,使这项工作?
这是我的SD将dumpstate写入文件android

File folder = new File(Environment.getExternalStorageDirectory()+"/IssueReport/"); 
    if (folder.isDirectory() == false) { 
     folder.mkdir(); 
    } 
    log = new File(Environment.getExternalStorageDirectory()+"/IssueReport/log.txt"); 

这里创建该文件是我在哪里文件写入到的位置

private void submit() { 
    try { 
     log.createNewFile(); 
     String cmd = "dumpstate "+log.getAbsolutePath(); 
     Runtime.getRuntime().exec(cmd); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 

回答

7

我得到它的工作。我发现Running Shell commands though java code on Android?并将其修改为像我需要的那样工作。

private void submit() { 
    try { 
     String[] commands = {"dumpstate > /sdcard/log1.txt"}; 
     Process p = Runtime.getRuntime().exec("/system/bin/sh -"); 
     DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
      for (String tmpCmd : commands) { 
       os.writeBytes(tmpCmd+"\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

如果有人需要它,这里是我一起运行一切的方式。该应用程序需要附加一个错误报告,从阅读Running Shell commands though java code on Android?,我看到没有办法运行一个错误报告,只有三个组件:dumpstate,dumpsys和log。我分别生成每个报告,然后将它们全部合并到一个文件中以附加到电子邮件。

private void submit() { 
    try { 
     String[] commands = {"dumpstate > /sdcard/IssueReport/dumpstate.txt", 
           "dumpsys > /sdcard/IssueReport/dumpsys.txt", 
           "logcat -d > /sdcard/IssueReport/log.txt", 
           "cat /sdcard/IssueReport/dumpstate.txt /sdcard/IssueReport/dumpsys.txt /sdcard/IssueReport/log.txt > /sdcard/IssueReport/bugreport.rtf" }; 
     Process p = Runtime.getRuntime().exec("/system/bin/sh -"); 
     DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
      for (String tmpCmd : commands) { 
        os.writeBytes(tmpCmd+"\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace();  
     } 
+0

执行此代码行Process p = Runtime.getRuntime()。exec(“/ system/bin/sh - ”);将所有日志放在一起?如果我创建一个空项目并执行submit()方法,它会写上一个应用程序粉碎报告吗? –