截取android返回Null的截图?

时间:2014-05-30 04:27:59

标签: android

我想使用此代码截取我的 Rooted 设备(不仅仅是特定活动,而是整个屏幕)的屏幕截图 -

try {

 Process sh = Runtime.getRuntime().exec("su", null,null);
            OutputStream  os = sh.getOutputStream();
            os.write(("/system/bin/screencap -p " + ""+mpath).getBytes("ASCII"));
            os.flush();
            os.close();
            sh.waitFor();
     } catch (Exception e)
      {
         Log.e("TAG_Err", "Error: "+e);
       }

,其中

mpath= Environment.getExternalStorageDirectory().toString() + "/screenshots.png";

现在尝试用 -

显示它
        try     
            {
            Bitmap bMap = BitmapFactory.decodeFile(""+mpath);
            ImageView image = (ImageView)findViewById(R.id.imageView1);
            image.setImageBitmap(bMap);
            }
            catch(Exception e)
            {
                Log.e("TAG_DISPLAY_ERR","error : "+e);
            }`

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是Image并没有像现在这样说 - 无法解码Stream - java.io.FileNotFound异常..我在sdcard中搜索过,没有名称为screenshots.png的文件/图像。

为什么? 这段代码有什么问题? 我该怎么办?

1 个答案:

答案 0 :(得分:0)

要保存文件,您可以使用

      OutputStream fOut = null;
                    File file = new File(directoyname,imagename);
                        fOut = new FileOutputStream(file);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                        fOut.flush();
                        fOut.close();

            MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());