Sometimes in our android application development we have requirement like taking a screenshot of whole screen or screenshot of fixed area. Following example explain how to take screenshot programmatically.
At first we need storage write permission because we will store screenshot in our device storage.
Code for write storage permission. Declare WRITE_EXTERNAL_STORAGE permission in AndroidManifest.
Next stop design and decide which area of screen should be considered while taking screenshot.
Here i have one main layout. Inside that one Button to take screenshot and another ScrollView. We take screenshot of ScrollView in which we have list of ImageView.
Here is the code to take screenshot. We ask for permission at run time on Button click if it not allowed previously. Then in AsyncTask we get height and width of our view and create bitmap using Canvas, write to our File Storage using FileOutputStream. That’ it. Screenshot capture successfully.