I want to develop an application that will take screenshot of android screen..does any one know how to do it..? which is similar to koushik duttas screen shot..But with out using root..and does any one had koushik dutta screenshot application which is working..? is not working for me..please let me know..thanks in advance.
4 Answers
Let's say that you clicked on a button:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
After that you need these two methods:
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}

- 1,908
- 5
- 30
- 49

- 614
- 4
- 16
-
1Should you use CompressFormat.PNG instead of CompressFormat.JPG since the file being saved is a .png? (or change the filename to .jpg) – joshaidan Apr 19 '13 at 20:23
-
this is just basic example of code. you can use what ever you want :) – Kristijan Drača Apr 20 '13 at 00:28
-
2I'm just saying, you have "screenshot.png" as the filename, but using CompressFormat.JPEG as the compress format. Inconsistency. – joshaidan May 10 '13 at 20:33
-
@joshaidan You can go and edit his answer if you want :) – fedmich Jan 27 '14 at 04:24
-
Note that this solution will sometimes return cached images (not always the "current" screen, when the button is clicked). See my answer for a method that works more consistently. – zpr Nov 10 '14 at 10:44
-
I think this solution takes a screenshot of the app and not the whole screen, meaning it will not show content of other apps if the initiating app is translucent, nor will it screen cap the notifications bar. – AlikElzin-kilaka Dec 22 '15 at 11:27
You can try something like this
private RelativeLayout view;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (RelativeLayout)findViewById(R.id.relativeView);
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
}

- 53
- 5
The method view.getDrawingCache() will first attempt to retrieve an image it has previously cached. This can cause issues if you want to guarantee your screenshot is up-to-date. For instance, if your user clicks your screenshot button, then changes the UI, then clicks it again, the second screenshot will be identical to the first unless you wipe the cache. I find the following method more convenient:
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
rootView.draw(canvas);
return bitmap;
}
I think it is impossible without root or the SDK, sorry.
I would love to be proven wrong.
Not an app, but if you have a USB cable, you can install the Android SDK on a PC and take screenshots from the PC with androidscreencast, without having to root your phone.

- 58,567
- 58
- 222
- 373
-
HI nichola, thanks for your reply..then do u had any working source code that works on rooted phone..?as the koushik duttas code is not working for me.. – Sri Sri Sep 17 '10 at 10:22