簡體   English   中英

在android中捕獲圖像時破壞管道錯誤

[英]broken pipe error while Capturing image in android

我將捕獲一個來自android中的應用程序的圖像,但是輸出流存在一些問題,並且使用JAVA.IO的BROKEN PIPE出錯 我的代碼如果在下面。 這是寫命令功能的問題。 我已經從git hub下載了這個演示版,所以請盡快幫助我。 非常感謝提前。

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());
    }

}

我認為您執行的進程已終止,因此OutputStream不再可用,因此出現“Broken Pipe”錯誤。

我會嘗試:

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

而不是直接打電話:

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

試試看。 讓我知道。

也許你可以從以下方面得到一些好主意:

帶輸入/輸出流的Java進程

以防萬一......你檢查過你的存儲卡是否正確? 如果你使用wifi - 連接好嗎?

我認為訪問存儲是一個問題,您應該查看uses-permission,也許您應該使用Environment.getExternalStorage.getPath來訪問要保存圖像的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM