title: Capture screen of SurfaceView
body: <p><img src="https://i.stack.imgur.com/ELw5W.png" alt="enter image description here"></p>
  
  <p>In above image i have one <code>SurfaceView</code> and one <code>Button</code> named <code>Capture</code>. <code>SurfaceView</code> is showing camera preview. So, I want screenshot of <code>surfaceview</code> when i click on <code>Capture</code> button. I had tried many examples and answers but none of them works. I just get <strong>Black Screenshot image</strong>. </p>
  
  <p>In some posts i also found it is impossible to take screenshot of <code>surfaceview</code>. So please help me any alternative solution with example.</p>
  
  <p>Here are some answers I already seen:</p>
  
  <p><a href="https://stackoverflow.com/questions/25091703/how-to-create-and-save-a-screenshot-from-a-surfaceview">how to create and save a screenshot from a surfaceview?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18289544/taking-screenshot-programmatically-doesnt-capture-the-contents-of-surfaceview">Taking screenshot programmatically doesnt capture the contents of surfaceVIew</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11505093/how-to-capture-screenshot-of-surfaceview-with-background">How to capture screenshot of surfaceview with background</a></p>
  
  <p>and many more.</p>
  
  <p>This is my activity call code:</p>
  
  <pre><code>public class CamView extends Activity implements SurfaceHolder.Callback {
  
      protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
  
      private SurfaceView SurView;
  
      private SurfaceHolder camHolder;
  
      private boolean previewRunning;
  
      final Context context = this;
  
      public static Camera camera = null;
  
      private RelativeLayout CamView;
  
      private Bitmap bmp1;
  
      @SuppressWarnings("deprecation")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_cam_view);
  
          CamView = (RelativeLayout) findViewById(R.id.camview);
          SurView = (SurfaceView) findViewById(R.id.sview);
          camHolder = SurView.getHolder();
          camHolder.addCallback(this);
          camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
          Button btn = (Button) findViewById(R.id.button1);
  
          btn.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  camera.takePicture(null, null, mPicture);
              }
          });
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
          if (previewRunning) {
              camera.stopPreview();
          }
          Camera.Parameters camParams = camera.getParameters();
          Camera.Size size = camParams.getSupportedPreviewSizes().get(0);
          camParams.setPreviewSize(size.width, size.height);
          camera.setParameters(camParams);
          try {
              camera.setPreviewDisplay(holder);
              camera.startPreview();
              previewRunning = true;
          }
          catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
          try {
              camera = Camera.open();
          }
          catch (Exception e) {
              e.printStackTrace();
              finish();
          }
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          camera.stopPreview();
          camera.release();
          camera = null;
      }
  
      public void TakeScreenshot() {
          Random num = new Random();
          int nu = num.nextInt(1000);
          CamView.setDrawingCacheEnabled(true);
          CamView.buildDrawingCache(true);
          Bitmap bmp = Bitmap.createBitmap(CamView.getDrawingCache());
          CamView.setDrawingCacheEnabled(false);
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          bmp.compress(CompressFormat.JPEG, 100, bos);
          byte[] bitmapdata = bos.toByteArray();
          ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata);
  
          String picId = String.valueOf(nu);
          String myfile = "anand" + picId + ".jpeg";
  
          File dir_image = new File(Environment.getExternalStorageDirectory() + File.separator + "anand");
          dir_image.mkdirs();
          try {
              File tmpFile = new File(dir_image, myfile);
              FileOutputStream fos = new FileOutputStream(tmpFile);
  
              byte[] buf = new byte[1024];
              int len;
              while ((len = fis.read(buf)) &gt; 0) {
                  fos.write(buf, 0, len);
              }
              fis.close();
              fos.close();
              bmp1 = null;
  
              camera.startPreview();
          }
          catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private PictureCallback mPicture = new PictureCallback() {
          @Override
          public void onPictureTaken(byte[] data, Camera camera) {
  
              File dir_image2 = new File(Environment.getExternalStorageDirectory() + File.separator + "anand");
              dir_image2.mkdirs();
              File tmpFile = new File(dir_image2, "TempPic.jpg");
              try {
                  FileOutputStream fos = new FileOutputStream(tmpFile);
                  fos.write(data);
                  fos.close();
              }
              catch (FileNotFoundException e) {
              }
              catch (IOException e) {
              }
  
              String path = (Environment.getExternalStorageDirectory() + File.separator + "anand" + File.separator + "TempPic.jpg");
  
              BitmapFactory.Options options = new BitmapFactory.Options();
              options.inPreferredConfig = Bitmap.Config.ARGB_8888;
              bmp1 = BitmapFactory.decodeFile(path, options);
  
              tmpFile.delete();
              TakeScreenshot();
          }
      };
  
      public Bitmap decodeFile(File f) {
          Bitmap b = null;
          try {
              BitmapFactory.Options o = new BitmapFactory.Options();
              o.inJustDecodeBounds = true;
  
              FileInputStream fis = new FileInputStream(f);
              BitmapFactory.decodeStream(fis, null, o);
              fis.close();
              int IMAGE_MAX_SIZE = 1000;
              int scale = 1;
              if (o.outHeight &gt; IMAGE_MAX_SIZE || o.outWidth &gt; IMAGE_MAX_SIZE) {
                  scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
              }
  
              BitmapFactory.Options o2 = new BitmapFactory.Options();
              o2.inSampleSize = scale;
              fis = new FileInputStream(f);
              b = BitmapFactory.decodeStream(fis, null, o2);
              fis.close();
          }
          catch (IOException e) {
              e.printStackTrace();
          }
          return b;
      }
  }
  </code></pre>
  
  <p>This is my layout code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"   
  android:id="@+id/camview"&gt;
  
  &lt;SurfaceView
      android:id="@+id/sview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" /&gt;
  
  &lt;Button
      android:id="@+id/button1"
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>