writing dumpstate to file android

Jasonwilliams10 picture Jasonwilliams10 · Jan 25, 2012 · Viewed 14k times · Source

I need the bugreport option that you can use in adb to go to a file on the sd in my app. I found Android; using exec("bugreport") that explains that you can not run bugreport in the regular shell and that you need to run dumpstate, dumpsys, and logcat separately to get the same result. That is fine and I understand that, but I can not get dumpstate or dumpsys to write to the file. The below works fine to write the logcat using logcat -d -f, but does not work for the other two. I have tried dumpstate -f , dumpstate -d -f and dumpstate > to get it work, but still does not write anything to the file. Is there something I am missing to make this work?
This is where I am creating the file on the sd

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

and here is where I am writing the file to the location

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

Answer

Jasonwilliams10 picture Jasonwilliams10 · Jan 28, 2012

I got it working. I found Running Shell commands though java code on Android? and modified it to work like I needed it to.

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();
        }

If anyone needs it, here is the way I am running everything together. The app needs to have a bug report attached to it, from reading Running Shell commands though java code on Android? , I saw that there is not a way to run a bug report, just the three componenets: dumpstate, dumpsys, and log. I am generating each report separately and then combining them all into one file to attach to an email.

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();    
        }