title: How to take a screenshot of Android's Surface View?
body: <p>I want to programatically take screen shot of my game, just as you'd get in Eclipse DDMS.</p>
  
  <p>Screenshot taken through the solution proposed here: <a href="https://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android">How to programmatically take a screenshot in Android?</a> and in most other SO questions only have View elements visible, but not the SurfaceView. </p>
  
  <pre><code>SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
  // Get root view
  View view = activity.getWindow().getDecorView().getRootView();
  // Create the bitmap to use to draw the screenshot
  final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
  final Canvas canvas = new Canvas(bitmap);
  
  // Get current theme to know which background to use
  final Theme theme = activity.getTheme();
  final TypedArray ta = theme
      .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
  final int res = ta.getResourceId(0, 0);
  final Drawable background = activity.getResources().getDrawable(res);
  
  // Draw background
  background.draw(canvas);
  
  // Draw views
  view.draw(canvas);
  
  // Save the screenshot to the file system
  FileOutputStream fos = null;
  try {
      final File sddir = new File(SCREENSHOTS_LOCATIONS);
      if (!sddir.exists()) {
          sddir.mkdirs();
      }
      fos = new FileOutputStream(SCREENSHOTS_LOCATIONS
              + System.currentTimeMillis() + ".jpg");
      if (fos != null) {
          if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
              Log.d("ScreenShot", "Compress/Write failed");
          }
          fos.flush();
          fos.close();
      }
  
  } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  
  <p>Any help in this regard would be highly appreciated. Thanks.</p>