java 截屏 - 后台服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44288546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take a screenshot - Background Service
提问by Mister JJ
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 聊天头,但我希望它在我单击时截取屏幕截图。
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 或更高版本。
采纳答案by Sarthak Doshi
For Lollipop and above you can use the MediaProjectionAPI of Google to take the screenshot but you need to ask for the permission from the user.
Lollipop 及以上版本可以使用谷歌的MediaProjectionAPI 截屏,但需要征得用户同意。
You can find the sample screen capture code using MediaProjection Here
您可以在此处使用 MediaProjection 找到示例屏幕截图代码
For the devices less then Lollipop you need root permission for it.
对于小于 Lollipop 的设备,您需要 root 权限。
回答by gjsalot
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?
回答by ADITYA RANA
Use MediaProjectionManager#createScreenCaptureIntent
given in the MediaProjection
API of Android. 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.
使用MediaProjectionManager#createScreenCaptureIntent
在MediaProjection
Android的API 中给出。您现在所做的是获取 chatHead 的根视图信息,而不是出现在您设备的整个屏幕上的信息,包括顶部的状态栏。