当我的应用程序每隔200毫秒安装并在后台运行并将图像保存到我的计算机中时,我需要以编程方式以Android设备或模拟器的屏幕截图。我已经使用下面的代码实现了此过程,并且仅在我的应用程序处于前景时才起作用。当我的应用程序在后台时我也想截图。下面是我的代码:Android - 如何以编程方式抓取屏幕截图
public static Bitmap takeScreenshot(Activity activity, int ResourceID) {
Random r = new Random();
int iterator=r.nextInt();
String mPath = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
View v1 = activity.getWindow().getDecorView().findViewById(ResourceID);
v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight());
v1.setDrawingCacheEnabled(true);
final Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
Bitmap resultBitmap = Bitmap.createScaledBitmap(bitmap, 640, 480, false);
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
imageFile.mkdirs();
imageFile = new File(imageFile+"/"+iterator+"_screenshot.png");
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
resultBitmap.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
我怎样才能实现Devices -> DDMS
编程的抓屏的刷新和保存按钮的功能?我可以做到吗?
这会吓坏我了,如果这甚至有可能。这样做的应用程序引起严重的安全问题。 –
**除非电话是固定的(* kitkat *)**,否则无法完成此操作。至于“严重的安全问题”,我认为其他地方存在更严重的安全问题。如果应用程序可以请求获取截图等权限,那不是什么大问题。 –
这不是一个截图或其他...这是每200毫秒的截图。基本上是一个5 FPS的视频。这可以轻松捕获手机上完成的所有事情。不要为了我自己的使用而做出反对(制作一个应用程序的视频),但给第三方应用程序允许截取屏幕截图会是一个长期的漏洞。 –