Take a Screenshot programmatically in Android - Androhub

Take Screenshot Banner

Take a Screenshot programmatically in Android

Today we are going to learn how to take screenshot programatically in Android. It will help you if you want to share any screen from your app to WhatsApp, Facebook, Hangout or any other app.

Before starting the article please go through Share Image and Text article it will help you in this article.

Example

In this tutorial, we are going to learn how to take Screenshot and share the taken Screenshot to other apps.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Now, open activity_main.xml layout and add the below code to it. In this layout we are taking some Button to take Screenshot and an ImageView to show taken ScreenShot.

4. Create an enum java class naming ScreenshotType and add two type FULL and CUSTOM. This class will help me to distinguish between type of screenshot that i need to take.

5. Now open your MainActivity.java and add the below code. In below class the code that is important for taking screenshot and sharing it are:

  • Take Screenshot : For Custom screenshot where you need to hide some view or show some view then there for hiding views you need to use visibility mode INVISIBLE instead of GONE because for GONE mode it won’t consider the view in frame and will not show up or hide while taking screenshot.

  • Share Screenshot

Full Code:

6. Finally create new java class naming ScreenshotUtils.java. In this class we add some static method which help to take screenshot, make directory and store taken screenshot.

  • Take Screenshot

  • Create Directory : Here you need to use getExternalFilesDir instead of getExternalStorageDirectory because when you uninstall app then the images/screenshots will deleted automatically.

  • Store Screenshot

Full Code:

7. Hurray!! Now you are all set up and can add this feature to your apps.

Thanks. :)

 

8 Comments

Jochen
Sunday, September 24th, 2017

THANK YOU! this works perfectly.
Jochen

Reza
Friday, July 6th, 2018

thank you so much man 🙂
God bless you

thomas
Tuesday, August 7th, 2018

using marshmallow, app crashes when clicking screenshot button

Dr. Droid
Tuesday, August 7th, 2018

Hi Thomas,

The app is crashing due to there is no Run-Time Permission for Marshmallow Device. You can check the below link to add this permission :

https://www.androhub.com/working-android-marshmallow-6-0-permissions/

Thanks

thomas
Tuesday, August 7th, 2018

thanks!~

Aman Rohilla
Tuesday, January 28th, 2020

Can you tell me how to take screenshot of videoView. I mean the current frame that is being played in the videoView.

Dr. Droid
Tuesday, January 28th, 2020

Hi Aman,

Please check this link : https://stackoverflow.com/questions/10736517/playing-video-on-textureview
here you have to use textureView to take screenshot.

Hope it helps.

Thanks

Anton
Friday, August 27th, 2021

Excellent, but some methods are deprecated, e.g. .setDrawaingCache(). I have a problem in that this code works if I run it in .onResume(), but if I use it later in the process, through the click button, certain views disseappaers in the screenshot. Specifically, mpandroid chart charts are gone. Any idea how to scrupulously ensure ALL view that there visible when screenshot is initiated, also become part of screenshot?

Post comment

Your email address will not be published. Required fields are marked *