title: How to show the camera preview on a SurfaceView?
body: <p>I am trying to open the camera hardware on a SurfaceView. In the layout, I created a SurfaceView and I open the camera as shown in the code below. When I run the code, the toast in the CameraAvailableCB shows up and says "onCameraAvailable" but nothing appears on the SurfaceView.</p>
  
  <p>How do I show the camera display on the SurfaceView?</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      this.mBtnCapture = (Button) findViewById(R.id.actMain_btn_capture);
      this.mSurfaceView = (SurfaceView) findViewById(R.id.actMain_surfaceView);
      this.mSurfaceHolder = this.mSurfaceView.getHolder();
      this.mSurfaceHolder.addCallback(this);
      this.mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
  
      try {
          cameraIDsList = this.mCameraManager.getCameraIdList();
  
          for (String id : cameraIDsList) {
              Log.v(TAG, "CameraID: " + id);
          }
  
      } catch (CameraAccessException e) {
          e.printStackTrace();
      }
  
      cameraStateCB = new CameraDevice.StateCallback() {
          @Override
          public void onOpened(CameraDevice camera) {
              Toast.makeText(getApplicationContext(), "onOpened", Toast.LENGTH_SHORT).show();
  
              //requesting permission
              int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
              if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
  
                  if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) {
  
                  } else {
                      ActivityCompat.requestPermissions(activity, 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();
              }
  
              //opening the camera
              try {
                  mCameraManager.openCamera(cameraIDsList[1], cameraStateCB, new Handler());
  
              } catch (CameraAccessException e) {
                  e.printStackTrace();
              }
          }
  
          @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();
          }
      };
  
      CameraManager.AvailabilityCallback cameraAvailableCB = new CameraManager.AvailabilityCallback() {
          @Override
          public void onCameraAvailable(String cameraId) {
              super.onCameraAvailable(cameraId);
  
              Toast.makeText(getApplicationContext(), "onCameraAvailable", Toast.LENGTH_SHORT).show();
          }
  
          @Override
          public void onCameraUnavailable(String cameraId) {
              super.onCameraUnavailable(cameraId);
  
              Toast.makeText(getApplicationContext(), "onCameraUnavailable", Toast.LENGTH_SHORT).show();
          }
      };
      this.mCameraManager.registerAvailabilityCallback(cameraAvailableCB, new Handler());
  }
  
  
  
  @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 (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED)
                  // Open Camera
                  break;
      }
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      Log.w(TAG, "surfaceCreated");
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      Log.w(TAG, "surfaceChanged");
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      Log.w(TAG, "surfaceDestroyed");
  }
  </code></pre>