Stream android logcat output to an sd card

后端 未结 4 709
孤街浪徒
孤街浪徒 2020-12-18 04:16

I want to achieve the following but so far, no luck

  • Open a file in the SD Card when the android application first started.
  • Stream the logcat output to
4条回答
  •  清歌不尽
    2020-12-18 04:36

    I repost my answer here so @JPM and others can see... The code basically just execute the logcat command and then build the log from the input stream.

    final StringBuilder log = new StringBuilder();
    try {        
        ArrayList commandLine = new ArrayList();
        commandLine.add("logcat");
        commandLine.add("-d");
        ArrayList arguments = ((params != null) && (params.length > 0)) ? params[0] : null;
        if (null != arguments){
            commandLine.addAll(arguments);
        }
    
        Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
        String line;
        while ((line = bufferedReader.readLine()) != null){ 
            log.append(line);
            log.append(LINE_SEPARATOR); 
        }
    } 
    catch (IOException e){
            //
    } 
    
    return log;
    

提交回复
热议问题