androidandroid-manifestfile-handlingsystrace

Write output of android shell command to a file


I am trying to write output of atrace to sdcard. I am using getRuntime().exec()("atrace gfx > /storage/sdcard0/trace.txt") in the app.App is signed as a system app and the command is working fine from terminal.But no file is created when running it from the app.


Solution

  • I solved it by reading data from inputstream and writing it to a file

     try {
    
    
                            Process p = Runtime.getRuntime().exec("atrace -t 5 gfx");
                            p.waitFor();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                            String line = reader.readLine();
                            File myFile = new File("/storage/sdcard0/trac.txt");
                            FileOutputStream f = null;
    
                            try {
                                f = new FileOutputStream(myFile);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            PrintWriter pw = new PrintWriter(f);
                            while (line != null) {
                                pw.println(line);
    
                                //Toast.makeText(getBaseContext(), line, Toast.LENGTH_LONG).show();
                                line = reader.readLine();
                            }
                            pw.flush();
                            pw.close();
                            f.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }