Java – How to take a screenshot of status bar content in android app?

How to take a screenshot of status bar content in android app?… here is a solution to the problem.

How to take a screenshot of status bar content in android app?

I have a

request that I have to take a screenshot of the current window in android.
This is the code I’m 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 it I can get screenshots of the app, but the status bar content is missing (Bluetooth icon, battery status, etc.).
How do I programmatically take a screenshot of the current Android phone window (including status bar content)?
Is it possible?

Solution

On Android 5.0+, use the Media Projection API to capture the device screen. See this sample app, because the process is a bit complicated.

On older Android devices, programmatic screenshots of the device screen are not supported.

Related Problems and Solutions