writing dumpstate to file android

文章详细介绍了如何在Android应用中使用Java代码执行bugreport命令,并将其输出到SD卡上的特定文件中。通过创建并运行自定义的shell命令,实现了将dumpstate、dumpsys和logcat报告整合到一个文件内的解决方案。

I need the bugreport option that you can use in adb to go to a file on the sd in my app. I foundAndroid; 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();
  }
link | improve this question

feedback

1 Answer

up vote 1 down vote accepted

I got it working. I foundSOLVED- Running Shell commands though java code on Androidand 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 readingSOLVED- 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();  
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值