Taking Screenshot Programmatically Using PixelCopy Api

Shivesh Karan Mehta
1 min readJan 26, 2019

--

Taking a screenshot programmatically in an Android Application can be very easy. It will be very helpful for developers when user reports something and send a visual form of that screen when a bug found.

Since these three constant DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_HIGH and DRAWING_CACHE_QUALITY_LOW are deprecated in API level 28. So we have to use something else and Now PixelCopy comes for rescue :-D

What is PixelCopy

Provides a mechanisms to issue PixelCopy requests to allow for copy operations from Surface to Bitmap Requires API level 24 (Android 7.0, Nougat)

How to use PixelCopy

We have to create an utility function, that way we can reuse that function anywhere, where we want to take screenshot, like this-

in this method we have to pass view, i.e. if we want to take screenshot of the whole layout than we have to pass rootView like this:-

Share the screenshot

To share the screenshot which we have taken just now, you will need to store it. So we are going to use the compress method of the Bitmap class.

That’s all, Now you can get screenshot and store the screenshot by using above method.

If you like it do share and like the tutorial :-)

--

--