I was trying to take screenshot of the Android screen programatically. I had done the following code:
private void getsnap(){
    try{
        Process sh = Runtime.getRuntime().exec("su", null, null);
        OutputStream os = sh.getOutputStream();
        String filePath = this.getFilesDir().getPath().toString() + "/fileName1.jpeg";
        os.write(("/system/bin/screencap -p " + filePath).getBytes("ASCII"));
        os.flush();
        os.close();
        sh.waitFor();       
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
java.io.IOException: write failed: EPIPE (Broken pipe)
Please can someone help? I had already checked the other posts and I dont find anything solving my issue.
EDIT:
Please note, the Error happens in the line os.write().
 
     
     
    