Android: write failed: EPIPE (Broken pipe) Error on write file

Hariprasauth Ramamoorthy picture Hariprasauth Ramamoorthy · Jul 12, 2013 · Viewed 34.1k times · Source

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().

Answer

Oleksandr Karaberov picture Oleksandr Karaberov · Jul 12, 2013

EPIPE issue usually happens when you either try to execute command which needs root permissions (getRuntime().exec) in your case on the device without it or run several root commands simultaneously. If you work on the emulator and need to root it I think you you can try this while the emulator is running:

adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system  
adb push su /system/xbin/su  
adb shell chmod 06755 /system  
adb shell chmod 06755 /system/xbin/su

Here http://abd-tech.blogspot.com/2011/05/test-root-apps-on-android-emulator.html more detail explanation.