Code example for Activity

Methods: runOnUiThreadgetResources

      throw new RuntimeException("Unable to capture screenshot.", e);
  private static void takeScreenshot(File file, final Activity activity) throws IOException {
    DisplayMetrics dm = activity.getResources().getDisplayMetrics();
    final Bitmap bitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, ARGB_8888);
    if (Looper.myLooper() == Looper.getMainLooper()) {
      // On main thread already, Just Do It™. 
      drawDecorViewToBitmap(activity, bitmap);
    } else { 
      // On a background thread, post to main. 
      final CountDownLatch latch = new CountDownLatch(1);
      activity.runOnUiThread(new Runnable() {
        @Override public void run() { 
          try { 
            drawDecorViewToBitmap(activity, bitmap);
          } finally { 
Contextual code suggestions in your IDE  Get Codota for Java