Take a Screenshot programmatically in Android

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. :)

Download Source Code – >

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (1)
Reply

THANK YOU! this works perfectly.
Jochen

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)