截图 - 后台服务
Take a screenshot - Background Service
我正在尝试使用后台服务截取屏幕截图。这个服务就像一个 Facebook 聊天头,但我希望它在我点击时截屏。
Preview Picture
我已经开发了一些代码,但它不起作用。我最后一次尝试的是:
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();
}
}
但是是截屏到我的按钮而不是屏幕。
我知道问题出在这里:
View v1 = chatHead.getRootView();
但我不知道如何修复它。谁能帮帮我?
我实际上使用的是 Android Studio 2.2.2 和 Android 4.0 或更高版本。
要获取包含不属于您的应用的视图的屏幕截图,您需要使用 MediaProjectionManager
。
见How to take a screen shot with status bar contents in android application?
Lollipop及以上版本可以使用Google的MediaProjectionAPI进行截图,但需征得用户同意
您可以使用 MediaProjection 找到示例屏幕捕获代码 Here
对于低于 Lollipop 的设备,您需要 root 权限。
使用 Android MediaProjection
API 中给出的 MediaProjectionManager#createScreenCaptureIntent
。您现在正在做的是获取 chatHead 的根视图信息,而不是设备整个屏幕上显示的信息,包括顶部的状态栏。
我正在尝试使用后台服务截取屏幕截图。这个服务就像一个 Facebook 聊天头,但我希望它在我点击时截屏。
Preview Picture
我已经开发了一些代码,但它不起作用。我最后一次尝试的是:
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();
}
}
但是是截屏到我的按钮而不是屏幕。
我知道问题出在这里:
View v1 = chatHead.getRootView();
但我不知道如何修复它。谁能帮帮我?
我实际上使用的是 Android Studio 2.2.2 和 Android 4.0 或更高版本。
要获取包含不属于您的应用的视图的屏幕截图,您需要使用 MediaProjectionManager
。
见How to take a screen shot with status bar contents in android application?
Lollipop及以上版本可以使用Google的MediaProjectionAPI进行截图,但需征得用户同意
您可以使用 MediaProjection 找到示例屏幕捕获代码 Here
对于低于 Lollipop 的设备,您需要 root 权限。
使用 Android MediaProjection
API 中给出的 MediaProjectionManager#createScreenCaptureIntent
。您现在正在做的是获取 chatHead 的根视图信息,而不是设备整个屏幕上显示的信息,包括顶部的状态栏。