简体   繁体   English

截取屏幕截图 - 后台服务

[英]Take a screenshot - Background Service

I'm trying to take a screenshot using a background service. 我正在尝试使用后台服务截取屏幕截图。 This service is like a Facebook chathead, but I want it to take an screenshot when I do a click. 这项服务就像一个Facebook聊天室,但我希望它在我点击时截取屏幕截图。

Preview Picture 预览图片

I've developed some code but it doesn't work. 我已经开发了一些代码,但它不起作用。 The last I've tried was: 我试过的最后一次是:

private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/capture/" + now + ".jpg";

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

        OutputStream fout = null;
        File imageFile = new File(mPath);

        try {
            fout = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
            fout.flush();
            fout.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

But is taking an screenshot to my button not to the screen. 但是截图到我的按钮而不是屏幕。

I know the problem is here: 我知道问题在这里:

View v1 = chatHead.getRootView();

But I don't know how to fix it. 但我不知道如何解决它。 Can anyone help me? 谁能帮我?

I'm actually using Android Studio 2.2.2 and Android 4.0 or greater. 我实际上使用的是Android Studio 2.2.2和Android 4.0或更高版本。

To get a screenshot containing views not belonging to your app you'll need to use the MediaProjectionManager . 要获取包含不属于您的应用的视图的屏幕截图,您需要使用MediaProjectionManager

See How to take a screen shot with status bar contents in android application? 请参阅如何在Android应用程序中使用状态栏内容拍摄屏幕截图?

For Lollipop and above you can use the MediaProjection API of Google to take the screenshot but you need to ask for the permission from the user. 对于Lollipop及以上版本,您可以使用Google的MediaProjection API来截取屏幕截图,但您需要征得用户的许可。

You can find the sample screen capture code using MediaProjection Here 您可以使用MediaProjection Here找到示例屏幕捕获代码

For the devices less then Lollipop you need root permission for it. 对于少于Lollipop的设备,您需要root权限才能获得它。

Use MediaProjectionManager#createScreenCaptureIntent given in the MediaProjection API of Android. 使用Android的MediaProjection API中提供的MediaProjectionManager#createScreenCaptureIntent What you are doing now is taking the chatHead's root view's information rather than information present on the complete screen of your device including the status bar at the top. 您现在正在做的是获取chatHead的根视图的信息,而不是设备整个屏幕上显示的信息,包括顶部的状态栏。

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

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