Mis*_* JJ 5 java android screenshot android-service
我正在尝试使用后台服务拍摄屏幕截图。该服务就像一个Facebook聊天头,但是我希望它在单击时拍摄屏幕截图。
我已经开发了一些代码,但是没有用。我尝试过的最后一个是:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是正在将屏幕截图带到我的按钮而不是屏幕上。
我知道问题在这里:
View v1 = chatHead.getRootView();
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何解决。谁能帮我?
我实际上正在使用Android Studio 2.2.2和Android 4.0或更高版本。
小智 6
对于 Lollipop 及以上版本,您可以使用Google的MediaProjection API 截取屏幕截图,但您需要征得用户的许可。
您可以在此处使用 MediaProjection 找到示例屏幕截图代码
对于小于 Lollipop 的设备,您需要 root 权限。
| 归档时间: |
|
| 查看次数: |
10426 次 |
| 最近记录: |