6

I have a requirement that I have to take a screenshot of the current window in android. This is the code am using,

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);

by using this I could get the application screen shot, but the status bar contents are missing (bluetooth icon, battery status etc). How can I take a screenshot of the current Android phone window programmatically (including status bar contents)? Is it possible?

Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
Vishnu Cyruz
  • 243
  • 1
  • 3
  • 14

1 Answers1

9

On Android 5.0+, use the media projection APIs to take a screenshot of the device screen. See this sample app, as the process is somewhat involved.

On older Android devices, there is no support for programmatic screenshots of the device screen.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @AndroidDev: The media projection APIs definitely work on Android 8.0 and higher. – CommonsWare Dec 28 '19 at 14:42
  • 1
    @AndroidDev: Note that I updated the link to [a newer version of that sample](https://github.com/commonsguy/cw-omnibus/tree/FINAL/MediaProjection/andshooter), which I am fairly certain works on Android 8.0. – CommonsWare Dec 28 '19 at 14:55