title: Take a photo with surfaceview
body: <p>I've tried to create an app that allows me to see in a SurfaceView what I see with the front Camera. I tried to implement a Button to allows me to take a screenshot of the Activity, because over the camera I have a mask. When I perform the screenshot I can capture only the screenshot of my mask that is over the surfaceView.
  I don't know what I'm doing wrong.
  Is there anyone can help me?
  My Intent is to create an app that allows me take a picture of my self whit a mask over my face.
  Her is the example of my virtual device.
  <a href="https://i.stack.imgur.com/upRsE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/upRsE.png" alt="enter image description here"></a>
  I've alredy tried with a real phone but nothing changed.  </p>
  
  <pre><code> public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Handler.Callback {
  
      static final String TAG = "CamTest";
      static final int MY_PERMISSIONS_REQUEST_CAMERA = 1242;
      private static final int MSG_CAMERA_OPENED = 1;
      private static final int MSG_SURFACE_READY = 2;
      private final Handler mHandler = new Handler(this);
      SurfaceView mSurfaceView;
      SurfaceHolder mSurfaceHolder;
      CameraManager mCameraManager;
      String[] mCameraIDsList;
      CameraDevice.StateCallback mCameraStateCB;
      CameraDevice mCameraDevice;
      CameraCaptureSession mCaptureSession;
      boolean mSurfaceCreated = true;
      boolean mIsCameraConfigured = false;
      private Surface mCameraSurface = null;
      Button btt_scatta;
      Bitmap immagine_screen;
      ImageView img_view_screen;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          //Richiedo il Fullscreen
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
          setContentView(R.layout.activity_main);
  
          //Get root view from Activity
          final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
  
          btt_scatta = (Button) findViewById(R.id.btt_scatta);
          img_view_screen = (ImageView) findViewById(R.id.img_view_screen);
          img_view_screen.setVisibility(View.GONE);
  
          this.mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
          this.mSurfaceHolder = this.mSurfaceView.getHolder();
          this.mSurfaceHolder.addCallback(this);
          this.mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
  
          try {
              mCameraIDsList = this.mCameraManager.getCameraIdList();
              for (String id : mCameraIDsList) {
                  Log.v(TAG, "CameraID: " + id);
              }
          } catch (CameraAccessException e) {
              e.printStackTrace();
          }
  
          mCameraStateCB = new CameraDevice.StateCallback() {
              @Override
              public void onOpened(CameraDevice camera) {
                  Toast.makeText(getApplicationContext(), "onOpened", Toast.LENGTH_SHORT).show();
  
                  mCameraDevice = camera;
                  mHandler.sendEmptyMessage(MSG_CAMERA_OPENED);
              }
  
              @Override
              public void onDisconnected(CameraDevice camera) {
                  Toast.makeText(getApplicationContext(), "onDisconnected", Toast.LENGTH_SHORT).show();
              }
  
              @Override
              public void onError(CameraDevice camera, int error) {
                  Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_SHORT).show();
              }
          };
  
          btt_scatta.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  btt_scatta.setVisibility(View.GONE);
                  immagine_screen = getScreenShot(rootView);
                  store(immagine_screen,"screenshot Honor");
                  img_view_screen.setVisibility(View.VISIBLE);
                  img_view_screen.setImageBitmap(immagine_screen);
              }
          });
      }
  
      @Override
      protected void onStart() {
          super.onStart();
  
          //requesting permission
          int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
          if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
              if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
  
              } else {
                  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
                  Toast.makeText(getApplicationContext(), "request permission", Toast.LENGTH_SHORT).show();
              }
          } else {
              Toast.makeText(getApplicationContext(), "PERMISSION_ALREADY_GRANTED", Toast.LENGTH_SHORT).show();
              try {
                  mCameraManager.openCamera(mCameraIDsList[1], mCameraStateCB, new Handler());
              } catch (CameraAccessException e) {
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          try {
              if (mCaptureSession != null) {
                  mCaptureSession.stopRepeating();
                  mCaptureSession.close();
                  mCaptureSession = null;
              }
  
              mIsCameraConfigured = false;
          } catch (final CameraAccessException e) {
              // Doesn't matter, cloising device anyway
              e.printStackTrace();
          } catch (final IllegalStateException e2) {
              // Doesn't matter, cloising device anyway
              e2.printStackTrace();
          } finally {
              if (mCameraDevice != null) {
                  mCameraDevice.close();
                  mCameraDevice = null;
                  mCaptureSession = null;
              }
          }
      }
  
      @Override
      public boolean handleMessage(Message msg) {
          switch (msg.what) {
              case MSG_CAMERA_OPENED:
              case MSG_SURFACE_READY:
                  // if both surface is created and camera device is opened
                  // - ready to set up preview and other things
                  if (mSurfaceCreated &amp;&amp; (mCameraDevice != null)
                          &amp;&amp; !mIsCameraConfigured) {
                      configureCamera();
                  }
                  break;
          }
  
          return true;
      }
  
      private void configureCamera() {
          // prepare list of surfaces to be used in capture requests
          List&lt;Surface&gt; sfl = new ArrayList&lt;Surface&gt;();
  
          sfl.add(mCameraSurface); // surface for viewfinder preview
  
          // configure camera with all the surfaces to be ever used
          try {
              mCameraDevice.createCaptureSession(sfl,
                      new CaptureSessionListener(), null);
          } catch (CameraAccessException e) {
              e.printStackTrace();
          }
  
          mIsCameraConfigured = true;
      }
  
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  
          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_CAMERA:
                  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
                      try {
                          mCameraManager.openCamera(mCameraIDsList[1], mCameraStateCB, new Handler());
                      } catch (CameraAccessException e) {
                          e.printStackTrace();
                      }
                  break;
          }
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          mCameraSurface = holder.getSurface();
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
          mCameraSurface = holder.getSurface();
          mSurfaceCreated = true;
          mHandler.sendEmptyMessage(MSG_SURFACE_READY);
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          mSurfaceCreated = false;
      }
  
      private class CaptureSessionListener extends
              CameraCaptureSession.StateCallback {
          @Override
          public void onConfigureFailed(final CameraCaptureSession session) {
              Log.d(TAG, "CaptureSessionConfigure failed");
          }
  
          @Override
          public void onConfigured(final CameraCaptureSession session) {
              Log.d(TAG, "CaptureSessionConfigure onConfigured");
              mCaptureSession = session;
  
              try {
                  CaptureRequest.Builder previewRequestBuilder = mCameraDevice
                          .createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                  previewRequestBuilder.addTarget(mCameraSurface);
                  mCaptureSession.setRepeatingRequest(previewRequestBuilder.build(),
                          null, null);
              } catch (CameraAccessException e) {
                  Log.d(TAG, "setting up preview failed");
                  e.printStackTrace();
              }
          }
      }
  
      //Capture the root view
      public static Bitmap getScreenShot(View view) {
          View screenView = view.getRootView();
          screenView.setDrawingCacheEnabled(true);
          Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
          screenView.setDrawingCacheEnabled(false);
          return bitmap;
      }
  
      //Store the Bitmap into the phone
      public static void store(Bitmap bm, String fileName){
          final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
          File dir = new File(dirPath);
          if(!dir.exists())
              dir.mkdirs();
          File file = new File(dirPath, fileName);
          try {
              FileOutputStream fOut = new FileOutputStream(file);
              bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
              fOut.flush();
              fOut.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>I think that a good solution is to take a picture with the camera and then put in a Image view under my mask, and then take a screenshot. But the problem is to take a photo with the surfaceView: I don't know how to do it.</p>