简体   繁体   English

在android中捕获图像时破坏管道错误

[英]broken pipe error while Capturing image in android

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 . 我将捕获一个来自android中的应用程序的图像,但是输出流存在一些问题,并且使用JAVA.IO的BROKEN PIPE出错 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. 我已经从git hub下载了这个演示版,所以请尽快帮助我。 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());
    }

}

I think the process you executed has been terminated, so the OutputStream is not available anymore, so the "Broken Pipe" error. 我认为您执行的进程已终止,因此OutputStream不再可用,因此出现“Broken Pipe”错误。

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 带输入/输出流的Java进程

Just in case... Did you check that your memory card is correct ? 以防万一......你检查过你的存储卡是否正确? if you use wifi - The connection is good ? 如果你使用wifi - 连接好吗?

我认为访问存储是一个问题,您应该查看uses-permission,也许您应该使用Environment.getExternalStorage.getPath来访问要保存图像的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM