Search code examples
androidscreenshot

android SurfaceControl screenshot return null


I'm developing a system application, and I need to take screenshot of the home screen.

In android 4.0 to 4.2, there's a hidden API in com.android.view.Surface, which works perfectly by calling Surface.screenshot(width, height), it returns the screenshot bitmap:

    public static Bitmap screenshot(int width, int height);

Since android 4.3, the screenshot() method had been moved to com.android.view.SurfaceControl. However, When I call SurfaceControl.screenshot(width, height) just as I did in android 4.2, it returns null.

The application has READ_FRAME_BUFFER permission, and is signed with platform certificate, and it has a sharedUserId of "android.uid.system". It works fine in 4.0 and 4.2, but return null in 4.4, no warning or error in logcat.

In android 4.0 to 4.2, the Surface class isn't hidden, just the method screenshot() is hidden. In android 4.4, the SurfaceControl class is hidden.

So, how can I make it work in android 4.4 ?


Solution

  • It seems to be the emulator's problem. The 4.2 emulator can take screenshot, the 4.4 emulator can not. But it works fine on real phone on both 4.2 and 4.4.