捕获屏幕Android,但不是我的应用

时间:2017-03-02 14:52:31

标签: android screen capture rootview

大家好我会尽量清楚....我有一个方法,我得到我的应用程序的屏幕,我真正想要的是能够使用我的方法从其他屏幕获取屏幕应用程序,或Android桌面,我尝试这样做的方式是在我捕获屏幕之前,我将我的布局转换为INVISIBLE,但我的捕获变黑,如果我捕获我的应用程序,如果它出来完美,但我想捕获其他应用程序,任何想法? ......我告诉你我的方法......

public void addListenerOnButton4(){

    Button btnTakeScreenshot = (Button) findViewById(R.id.share);


    btnTakeScreenshot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takeScreenshot();
        }
    });
}
public void takeScreenshot() {

    RelativeLayout ln = (RelativeLayout) findViewById(R.id.Layout);
    ln.setBackgroundColor(Color.TRANSPARENT);
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = (Environment
                .getExternalStoragePublicDirectory(Environment
                        .DIRECTORY_DOWNLOADS) +File.separator+now+"ScreenShoot.jpg");


        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

- API级别21 +

您可以使用MediaProjectionManagerDetails here.

MediaProjection实现为您提供了Bitmap,您可以将其另存为JPEG。获得byteArray后,就像通常用Java一样将其写入文件。

Bitmap bmp = // your Bitmap here
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

- 21之前的API

Android提供了一种仅捕获View的方法,但当您INVISIBLE时,View再也没有颜色了。这就是你得到黑屏的原因。 由于捕获功能与View相关联,因此您无法截取其他应用只是让View不可见。

- 任何API +根

如果你想要这样做,你需要root访问权限。然后,您可以阅读framebuffer并获取原始数据并将其转换为BitmapCheck details here.