broken pipe error while Capturing image in android
Asked Answered
D

3

14

I am going to capture an image which is from an application in android, but there is some problem with Output Stream and there is an error with BROKEN PIPE with JAVA.IO. My code if below. Here is the problem with the write command function. I have downloaded this demo from git hub, so please help me as soon as possible. Thanks a lot in advance.

void takeScreenshot()
{
    String file ="/data/data/com.koushikdutta.screenshot/screenshot.bmp";;
    OutputStream os = null;
    int screenshotUid;
    String screenshotUser = null;
    Process sh = null;
    try
    {
        try
        {
            sh = Runtime.getRuntime().exec("su -c sh");

            os = sh.getOutputStream();
            Log.e("","THE VALUE OF OBJECT IS:::::"+os.toString());

            // file ="/data/data/com.koushikdutta.screenshot/screenshot.bmp";
            screenshotUid = getUidForPackage("com.koushikdutta.screenshot");
            screenshotUser = getUserForPackage("com.koushikdutta.screenshot");
        }
        catch(Exception e)
        {
            Log.e("","Hi Error created");
        }
        try
        {
            Thread.sleep(2000);
            writeCommand(os, "rm "+file);
            writeCommand(os, "mkdir /sdcard/dcim");

        }
        catch(Exception e)
        {
            Log.e("","Hello How are you??"+e.getMessage());
        }
        writeCommand(os, "mkdir /sdcard/dcim/Screenshot");
        writeCommand(os, "/data/data/com.koushikdutta.screenshot/screenshot");
        writeCommand(os, "chown root." + screenshotUser + " " + file);
        writeCommand(os, "chmod 660 " + file);
        writeCommand(os, "exit");
        os.flush();
        os.close(); 
        boolean success = false;
        for (int i = 0; i < 10; i++)
        {
            try
            {
                Thread.sleep(1000);
                // if we can successfully get the exit value, 
                // then that means the process exited.
                sh.exitValue();
                success = true;
                break;
            }
            catch (Exception ex)
            {
                Log.e("","Error while"+ex.getMessage());
            }
        }
        try
        {
            if (!success)
                throw new Exception("Unable to take screenshot");

            File screenshot = new File(file);
            if (!screenshot.exists())
                throw new Exception("screenshot.raw file not found!");

            mHander.post(new Runnable()
            {
                public void run()
                {
                    Toast toast = Toast.makeText(
                        ScreenshotActivity.this, 
                        "Screen captured!", Toast.LENGTH_LONG);
                    toast.show();
                }
            });
        }
        catch(Exception e)
        {
            Log.e("","ERROR CREATING......."+e.getMessage());
        }

        try
        {
            FileInputStream fs = new FileInputStream(file);
            mBitmap = BitmapFactory.decodeStream(fs);
            mScreenshotFile = String.format(
                        "/sdcard/dcim/Screenshot/screenshot%d.png", 
                        System.currentTimeMillis());
            FileOutputStream fout = new FileOutputStream(mScreenshotFile);
            mBitmap.compress(CompressFormat.PNG, 100, fout);
            fout.close();
            mConnection.scanFile(mScreenshotFile, null);
        }
        catch (Exception ex)
        {
            Log.e("","Error while"+ex.getMessage());
        }

        mHander.post(new Runnable()
        {
            public void run()
            {
                mImage.setImageBitmap(mBitmap);
            }
        });
    }
    catch (Exception ex)
    {
        Toast toast = Toast.makeText(
                        ScreenshotActivity.this, "Error: " + ex.getMessage(), 
                        Toast.LENGTH_LONG);
        toast.show();
        Log.e("",""+ "Error: " + ex.getMessage());
    }
}

   static void writeCommand(final OutputStream os, String command) 
   {

       try
    {
        os.write((command+"\n").getBytes("ASCII"));
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Log.e("","Error was::::::::::::::::::"+e.getMessage());
    }

}
Drucilladrucy answered 3/4, 2013 at 9:37 Comment(0)
I
1

I think the process you executed has been terminated, so the OutputStream is not available anymore, so the "Broken Pipe" error.

I would try:

ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectErrorStream(true);
Process process = builder.start();

instead of calling directly:

sh = Runtime.getRuntime().exec("su -c sh");

give it a try. Let me know.

Maybe you can get some good ideas from:

Java Process with Input/Output Stream

Iminourea answered 3/4, 2013 at 10:38 Comment(1)
java.io.IOException: Error running exec(). Command: [/bin/bash] Working Directory: null Environment: [ANDROID_SOCKET_zygote=10, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, PATH=/sbin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar, ANDROID_PROPERTY_WORKSPACE=9,32768, ANDROID_ROOT=/system, LD_LIBRARY_PATH=/system/lib]Drucilladrucy
W
0

Just in case... Did you check that your memory card is correct ? if you use wifi - The connection is good ?

Welcher answered 10/4, 2013 at 22:46 Comment(0)
K
0

I think is a problem with the access to storage you should review the uses-permission and perhaps you should use the Environment.getExternalStorage.getPath to access at place where you want to save the image.

Kiangsu answered 17/3, 2015 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.