Skip to content

Instantly share code, notes, and snippets.

@PDDStudio
Created January 20, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PDDStudio/36383f6fecf815c7ee7a to your computer and use it in GitHub Desktop.
Save PDDStudio/36383f6fecf815c7ee7a to your computer and use it in GitHub Desktop.
Take a screenshot programatically in Android
public static Bitmap takeScreenshot(Activity activity) {
ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
decorChild.setDrawingCacheEnabled(true);
decorChild.buildDrawingCache();
Bitmap drawingCache = decorChild.getDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
decorChild.setDrawingCacheEnabled(false);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment