當我的應用程序每隔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的視頻。這可以輕鬆捕獲手機上完成的所有事情。不要爲了我自己的使用而做出反對(製作一個應用程序的視頻),但給第三方應用程序允許截取屏幕截圖會是一個長期的漏洞。 –