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" />

31 thoughts on “Taking a screenshot of current Activity in Android

  1. 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

    1. 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.

  2. 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,

  3. 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.

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

    1. 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)
  4. 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

  5. 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

  6. 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?

  7. 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…..

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

  9. 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.

  10. Hi .. I am using this code .. Everything working good but i want to send the image to another activity .how do i do that .. plz help me.

  11. Hey I am the one who asked you on twitter I posted my question if you kindly can reply as soon as possible plz 🙂 .. This is the question:
    How can I do it if I want to screenshot the whole activity, I mean including the text that is not being shown (have to scroll up or down)? Thank you very much in advance

  12. I’m getting this error when i try to add the permission to my manifest.. any suggestions?.. “The prefix “android” for attribute “android:name” associated with an element type “uses-permission” is not bound.”

  13. I used this code. But I am unable to get screen shot. I am not getting any errors. And no image i found.

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 )

Facebook photo

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

Connecting to %s