I'm trying to take a screenshot from a background accessibility service of the whole screen, I tried to insert a view from the service that is drawn over the current application and try to do it by using that.But what I get is only my view with a black background. Any help is appreciated, thank you :D
public void addView() {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
if (layout != null) {
windowManager.removeViewImmediate(layout);
} else {
layout = (RelativeLayout) inflater.inflate(R.layout.hoock_layout, null);
layout.setDrawingCacheEnabled(true);
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
windowManager.addView(layout, params);
store(getScreenShot(layout), "TEST.png");
}
public Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
screenView.measure(View.MeasureSpec.makeMeasureSpec(1500, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(1800, View.MeasureSpec.UNSPECIFIED));
screenView.layout(0, 0, screenView.getMeasuredWidth(), screenView.getMeasuredHeight());
screenView.buildDrawingCache(true);
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
return bitmap;
}
public void store(Bitmap bm, String fileName) {
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AccessibilityPOF";
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}