Code example for Activity

Methods: runOnUiThread, getResources

0
      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 { 
            latch.countDown();
          }