Today, a snippet to present you how to programmatically take a screenshot in Android. You can take screenshot of a particular View and its children but in our case, we would like to take a screenshot of the entire window.

So, we must to get the root view of the window thanks to following calls in an Activity :


View v = getWindow().getDecorView().getRootView();

Now, we must enable drawing cache on View v and access to content thanks to getDrawingCache method. Don’t forget after this call to disable drawing cache on the View v.

Rest of the code consists to write the created Bitmap in a dedicated file and then you can use the screenshot contained in the file like you want in your application.

Snippet code is like this :


String path = Environment.getExternalStorageDirectory().toString() + "/" + FILENAME;

View v = getWindow().getDecorView().getRootView();
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);

OutputStream out = null;
File imageFile = new File(path);

try {
  out = new FileOutputStream(imageFile);
  // choose JPEG format
  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
  out.flush();
} catch (FileNotFoundException e) {
  // manage exception
} catch (IOException e) {
  // manage exception
} finally {

  try {
    if (out != null) {
      out.close();
    }

  } catch (Exception exc) {
  }

}