Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to capture a screenshot?

I know that there are a lot of questions about capturing screenshots, and I have checked most of them. They have the same answer (with small code variations).

I have following method for screenshot capturing:

@NonNull
public static Bitmap takeScreenShot(Window window) throws IOException {
    final View rootView = window.getDecorView().getRootView();
    final boolean drawingCacheEnabled = rootView.isDrawingCacheEnabled();
    rootView.setDrawingCacheEnabled(true);

    try {
        return Bitmap.createBitmap(rootView.getDrawingCache());
    } finally {
        rootView.setDrawingCacheEnabled(drawingCacheEnabled);
    }
}

And you can use it like these: takeScreenShot(getActivity().getWindow())

However these approach has several limitations:

  1. If you have some dialogs on the screen they will not be captured on screenshot.
  2. Will it work with hardware accelerated views? According to documentation:

    When hardware acceleration is turned on, enabling the
    drawing cache has no effect on rendering because the system uses a
    different mechanism for acceleration which ignores the flag

  3. Screenshot contains black boxes instead of GLviews. (e.g. when you app has maps.). It seems to be as a result of 2nd point.

So my question is, is there any solution without rooting that can solve at least some of my issues?

like image 987
Rostyslav Roshak Avatar asked Nov 07 '22 14:11

Rostyslav Roshak


1 Answers

Check out the following GitHub repo (not mine!): https://github.com/AndroidDeveloperLB/ScreenshotSample

Also, the following will be useful reading: How to properly take a screenshot, globally?

like image 126
Richard Avatar answered Nov 14 '22 21:11

Richard