【发布时间】:2013-11-22 02:52:35
【问题描述】:
当我的应用程序安装并在后台每 200 毫秒运行时,我需要以编程方式截取 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 中实现屏幕捕获的刷新和保存按钮的功能?我能做到吗?
【问题讨论】:
-
如果这可能的话,我会吓坏的。这样做的应用会引发严重的安全问题。
-
除非手机已root(kitkat),否则无法执行此操作。至于“严重的安全问题”,我认为其他地方存在更严重的安全问题。如果应用程序可以请求截取屏幕截图的权限,这没什么大不了的。
-
这不是屏幕截图……这是每 200 毫秒的屏幕截图。本质上是一个 5 FPS 的视频。这将轻松捕获在手机上完成的所有内容。不反对为我自己的使用(制作应用程序的视频)而这样做,但授予第 3 方应用程序截取屏幕截图的权限将是一个漫长的兔子洞。
-
stackoverflow.com/a/54779181/7074112 检查此答案以获得可能的解决方案