Home > Tutorials > Taking a screenshot of current Activity in Android

Taking a screenshot of current Activity in Android

In this post I’ll show how you can take a screenshot of your current Activity and save the resulting image on /sdcard.

The idea behind taking a screenshot actually is pretty simple: what we need to do is to get a reference of the root view and  generate a bitmap copy of this view.


Considering that we want to take the screenshot when a button is clicked, the code will look like this:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       Bitmap bitmap = takeScreenshot();


First of all we should retrieve the topmost view in the current view hierarchy, then enable the drawing cache, and after that call getDrawingCache().

Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invoking  getDrawingCache().

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   return rootView.getDrawingCache();


And the method that saves the bitmap image to external storage:

 public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);


Since the image is saved on external storage, the WRITE_EXTERNAL_STORAGE permission should be added AndroidManifest to file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. June 13, 2013 at 6:04 am

    tnx men.helps me a lot

  2. denis
    August 12, 2013 at 2:53 pm

    a huge thnx to you man! :)

  3. ken
    September 30, 2013 at 8:44 am

    hi sir.please help me. i use ur code in my app.i try to screenshot on playing video that stream from server by using webcam.but,when i click the screenshot button the video content turn black sir.do u know why?thx

    • September 30, 2013 at 5:42 pm

      I’m not sure, but I guess this has something to do with the SurfaceView, the view where the video is drawn.

      Perhaps the MediaPlayer (or other media related classes) should provide you with an API to get a picture of the current frame.

  4. Inna Tarasyan
    January 26, 2014 at 12:41 pm

    Hi everyone!!! Here could be a problem…when you created the AVD, don’t forget to specify the size of the SD card to make it writeable…Otherwise…. you will see exceptions..

    Best Regards,

  5. March 8, 2014 at 8:02 am

    hi everyone, i need help please, i have added the code to my project but when i click the button, nothing happens, i checked and theres no image saved and i dont get any errors in eclipse or my phone.

    • March 10, 2014 at 4:23 am

      Ok nevermind, i found the problem, the sd card needed to be refreshed, thanx

      • March 10, 2014 at 4:28 am

        Sorry that was to Re mount the SD Card, this code, sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+ Environment.getExternalStorageDirectory())));

  6. umit
    May 22, 2014 at 5:19 am

    instead whole screen view, can we just get rootview for an imageView in Activity?

    • May 23, 2014 at 6:48 am

      Sure, you can supply the id of any view to get a snapshot of it.
      For the imageView it will look something like this:

      View view = findViewById(R.id.my_imageView)
  7. Amit
    June 24, 2014 at 3:56 am

    Hey dats a great script…thumbs up for dat…
    I am having a slight problem though, when the snapshot is taken it does not include the whole screen..it just takes the activity and not the notification bar..can u suggest anything??
    thnx in advance

  8. John K
    June 30, 2014 at 11:12 am

    You are saving a .png image that contains JPEG formatted data.

    Nevertheless nice tutorial :)

  9. Nghe An
    July 15, 2014 at 3:02 pm

    I want an example screen capture is complete code sharing measure. Help me bared!

  10. Niraj Chauhan
    August 24, 2014 at 7:33 am

    Your code works like a charm, thanks :)..
    Can you please tell me how to take screenshot of just a particular linear layout? And when i tried to take screenshot of a view, the textview present in that view didnt came in the screenshot only the view came… Please help me I am new in Android

    • August 26, 2014 at 11:57 am

      Just use the id of your LinearLayout, instead of androids content.

      View rootView = findViewById(R.id.my_linear_layout);
  11. rajendran
    November 26, 2014 at 6:17 am

    it working fine.. but i added Google map.. using this its comes blank..
    can u help..me

  12. Kaura
    December 5, 2014 at 4:42 am

    Hi Thanks a lot for this tutorial. I am new to android and I need help. My android device(Nexus 9 ) only have internal storage. How can I store the screenshot in it?

    • Kaura
      December 5, 2014 at 5:12 am

      I am trying to use your code but its not working for me. Can you please help.

  13. April 22, 2015 at 8:35 am

    Thnxx for such type great article … But it captures only visible area of screen. Suppose i have a long scrollable Details so in that case how can i capture Full Capture Image…..

  14. Adrita
    July 10, 2015 at 10:38 am

    What if I want to save the image in any specific foder?

  15. Charoon
    July 23, 2015 at 10:37 am

    Its work fiine until i put the whole code into a thread. After that, this rootView.getDrawingCache(); return a null; Any idea?

  16. August 31, 2015 at 10:37 am

    Hi i am getting the below error while i am trying to invoke the code.

    java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.view.Window.getDecorView()’ on a null object reference

    We are invoking the code from javascript as a third party library.

  17. November 14, 2015 at 8:38 pm

    For FrameLayout yes, for GLSurfaceView or any other view that is backed by OpenGL it won’t work.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 587 other followers

%d bloggers like this: