截取屏幕截图 - 后台服务 [英] Take a screenshot - Background Service

查看:566
本文介绍了截取屏幕截图 - 后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用后台服务截取屏幕截图。这项服务就像一个Facebook聊天,但是我希望它在我点击一下时截取屏幕截图。

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.

预览图片

我开发了一些代码,但它不起作用。我试过的最后一次是:

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.

我知道问题在这里:

View v1 = chatHead.getRootView();

但我不知道如何修复它。任何人都可以帮助我吗?

But I don't know how to fix it. Can anyone help me?

我实际上使用的是Android Studio 2.2.2和Android 4.0或更高版本。

I'm actually using Android Studio 2.2.2 and Android 4.0 or greater.

推荐答案

要获取包含不属于您的应用的视图的屏幕截图,您需要使用 MediaProjectionManager

To get a screenshot containing views not belonging to your app you'll need to use the MediaProjectionManager.

参见如何在Android应用程序中使用状态栏内容进行屏幕截图?

这篇关于截取屏幕截图 - 后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆