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() { @Override public void onClick(View v) { Bitmap bitmap = takeScreenshot(); saveBitmap(bitmap); } });
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(); rootView.setDrawingCacheEnabled(true); 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); fos.flush(); fos.close(); } 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" />
tnx men.helps me a lot
a huge thnx to you man! 🙂
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
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.
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,
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.
Ok nevermind, i found the problem, the sd card needed to be refreshed, thanx
Sorry that was to Re mount the SD Card, this code, sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+ Environment.getExternalStorageDirectory())));
instead whole screen view, can we just get rootview for an imageView in Activity?
Sure, you can supply the id of any view to get a snapshot of it.
For the imageView it will look something like this:
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
You are saving a .png image that contains JPEG formatted data.
Nevertheless nice tutorial 🙂
I want an example screen capture is complete code sharing measure. Help me bared!
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
Just use the id of your LinearLayout, instead of androids content.
eg.:
it working fine.. but i added Google map.. using this its comes blank..
can u help..me
It won’t work for Google Maps API V2 because of the SurfaceView and OpenGL. You have to use instead:
Take a look over this stackoverlow link for an example usage:
http://stackoverflow.com/questions/13773658/capture-screen-shot-of-googlemap-android-api-v2/
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?
I am trying to use your code but its not working for me. Can you please help.
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…..
What if I want to save the image in any specific foder?
Its work fiine until i put the whole code into a thread. After that, this rootView.getDrawingCache(); return a null; Any idea?
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.
For FrameLayout yes, for GLSurfaceView or any other view that is backed by OpenGL it won’t work.
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.
Send the image path instead
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
If your content is wrapped in a ScrollView, then you can use the technique showed in this answer: http://stackoverflow.com/a/29000673.
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.”
I used this code. But I am unable to get screen shot. I am not getting any errors. And no image i found.
Hello, im having the same issue, did you would the solution??