1

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();
    }
}
Razvan Dalca
  • 31
  • 1
  • 6
  • 1
    use android.media.projection to capture screen. – Usman Rana Jul 28 '17 at 12:04
  • 1
    Hey, thanks for answering but, the problem there is the request, this app will be a parent advisor app and it will be installed on the child's phone and if I ask the child for permission, I risk of not getting it.Is there another way? – Razvan Dalca Jul 28 '17 at 12:07
  • There is no risk of not getting the permission as long as that behavior (of capturing the screen) is expected. – Android Admirer Jul 28 '17 at 12:10
  • 1
    The child will not expect it, the parent knows when installing the app that he will want to take remote screenshots of the child's device, but if i prompt the child to approve the permission he will not give it. To reformulate can i get this permission only one at the onboarding stage of the app ? Will the permission be required every time i want to take a print screen ? – Razvan Dalca Jul 28 '17 at 12:12
  • otherwise you can't capture screenshot in background. And as far as permission is concerned , it is one time process only. Allow a permission and use the app lifetime without issue – Usman Rana Jul 28 '17 at 12:18
  • OK, thanks so much. Have a nice day :D i'll try to implement this way then – Razvan Dalca Jul 28 '17 at 12:30
  • Did you found the solution for this?. I'm trying to take a screenshot using accessibility service. but I'm not able to find any documentation for it. Can you post your solution here if you got the answer to this question. @RazvanDalca – Siva Apr 03 '18 at 00:42
  • @UsmanRana Can you please check this question and answer it : https://stackoverflow.com/questions/49620758/android-take-screenshot-of-notification-bar-programatically – Siva Apr 06 '18 at 07:49

0 Answers0