title: SurfaceView Camera Preview Code working well on Pre Lollipop but Crashing on lollipop Devices
body: <p>I implemented  a custom SurfaceView to Draw Camera Preview and did all the operations of Capturing, and Manual Focusing on it. It works pretty well on Pre-Lolipop Devices but the problem is that it Crashes on Lollipop Devices. 
   The Most Strange thing that happens is, The Application throws an ANR and when I click Ok, the Application starts functioning from the point I had left it.<br>
  The LogCat shows <em>"Failed to Connect to Camera Error While After skipping ANR, I can easily connect and even Manipulate my Camera"</em>. Is it  because I have used the deprecated Camera API And not the Camera2 Along with Lollipop Devices?? </p>
  
  <p>My code goes like this: </p>
  
  <pre><code>public class CameraPreview
  implements
  SurfaceHolder.Callback {
  private int cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
  private Camera mCamera = null;
  public Camera.Parameters params;
  private SurfaceHolder sHolder;
  private String TAG="CameraPreview";
  public List&lt;Camera.Size&gt; supportedSizes;
  
  public int isCamOpen = 0;
  public boolean isSizeSupported = false;
  private int previewWidth, previewHeight;
  private List&lt;String&gt; mSupportedFlashModes;
  private boolean flashon=false;
  private final static String MYTAG = "CameraPreview";
  private ProgressDialog loading;
  
  public CameraPreview(int width, int height) {
      Log.i("campreview", "Width = " + String.valueOf(width));
      Log.i("campreview", "Height = " + String.valueOf(height));
      previewWidth = width;
      previewHeight = height;
  }
  
  private int openCamera() {
      if (isCamOpen == 1) {
          releaseCamera();
      }
  
      mCamera = Camera.open(cameratype);
  
      if (mCamera == null) {
          return -1;
      }
  
      if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
          mCamera.setDisplayOrientation(90);
      } else {
          mCamera.setDisplayOrientation(0);
      }
  
      params = mCamera.getParameters();
      params.setPreviewSize(previewWidth, previewHeight);
  
      try {
          mCamera.setParameters(params);
      } catch (RuntimeException e) {
          e.printStackTrace();
      }
  
      mCamera.startPreview();
      try {
          mCamera.setPreviewDisplay(sHolder);
      } catch (IOException e) {
          mCamera.release();
          mCamera = null;
          return -1;
      }
      isCamOpen = 1;
      return isCamOpen;
  }
  public int isCamOpen() {
      return isCamOpen;
  }
  
  public void releaseCamera() {
      if (mCamera != null) {
          mCamera.stopPreview();
          mCamera.setPreviewCallback(null);
          mCamera.release();
          mCamera = null;
      }
      isCamOpen = 0;
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      sHolder = holder;
      isCamOpen = openCamera();
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) {
  
  }
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  
      releaseCamera();
  
  }
  
  /**
   * Called from PreviewSurfaceView to set touch focus.
   * 
   * @param - Rect - new area for auto focus
   */
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public void doTouchFocus(final Rect tfocusRect) {
      Log.i(TAG, "TouchFocus");
      try {
          final List&lt;Camera.Area&gt; focusList = new ArrayList&lt;Camera.Area&gt;();
          Camera.Area focusArea = new Camera.Area(tfocusRect, 1000);
          focusList.add(focusArea);
  
          Camera.Parameters para = mCamera.getParameters();
          para.setFocusAreas(focusList);
          para.setMeteringAreas(focusList);
          mCamera.setParameters(para);
  
          mCamera.autoFocus(myAutoFocusCallback);
      } catch (Exception e) {
          e.printStackTrace();
          Log.i(TAG, "Unable to autofocus");
      }
  
  }
  
  /**
   * AutoFocus callback
   */
  AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
  
        @Override
        public void onAutoFocus(boolean arg0, Camera arg1) {
         if (arg0){
          mCamera.cancelAutoFocus();      
         }
      }
  };
  
  
  
  
  public void capturePicture(){
      mCamera.takePicture(null, null, mPicture);
  
  
  }
  
  private File getOutputMediaFile(){
  
      File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "UltimateCameraGuideApp");
  
      if (! mediaStorageDir.exists()){
          if (! mediaStorageDir.mkdirs()){
              Log.d("Camera Guide", "Required media storage does not exist");
              return null;
          }
      }
  
      // Create a media file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      File mediaFile;
      mediaFile = new File(mediaStorageDir.getPath() + File.separator +
              "IMG_"+ timeStamp + ".jpg");
  
      //DialogHelper.showDialog("Success!", "Your picture has been saved!", TouchActivity.reference.getActivity());
  
      return mediaFile;
  }
  
  private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
  
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
  
          //This One is Just for Getting a File Named after it
           loading=new ProgressDialog(BaseImagesContainer.reference);
          loading.setMessage("Getting Image Ready");
          loading.show();
          File pictureFile =getOutputMediaFile();
          if (pictureFile == null){
              Toast.makeText(TouchActivity.reference.getActivity(), "Image retrieval failed.", Toast.LENGTH_SHORT)
                      .show();
              return;
          }
          Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
          if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
              bmp=rotateImage(90,bmp);
          }else{
              bmp=rotateImage(270,bmp);
  
          }
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bmp.compress(Bitmap.CompressFormat.PNG,1, stream);
          byte[] flippedImageByteArray = stream.toByteArray();
          try {
              FileOutputStream fos = new FileOutputStream(pictureFile);
              fos.write(flippedImageByteArray);
              fos.close();
              // Restart the camera preview.
              //safeCameraOpenInView(mCameraView);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          Uri destination = Uri.fromFile(new File(TouchActivity.reference.getActivity().getCacheDir(), "cropped"));
          Uri source = Uri.fromFile(new File(pictureFile.getPath()));
          Crop.of(source, destination).withMaxSize(800,800).start(TouchActivity.reference.getActivity());
      }
  };
  
  
  public Bitmap rotateImage(int angle, Bitmap bitmapSrc) {
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(bitmapSrc, 0, 0,
              bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
  }
  
  
  public void switchCamera(){
  
      mCamera.stopPreview();
      //NB: if you don't release the current camera before switching, you app will crash
      mCamera.release();
  
      //swap the id of the camera to be used
      if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
          cameratype=Camera.CameraInfo.CAMERA_FACING_FRONT;
      }else{
          cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
      }
      try{
          mCamera = Camera.open(cameratype);
      }catch (Exception e){
          e.printStackTrace();
          Toast.makeText(TouchActivity.reference.getActivity(),"Can't Open the Camera",Toast.LENGTH_LONG).show();
      }
  
      if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
  
          mCamera.setDisplayOrientation(90);
  
      } else {
  
          mCamera.setDisplayOrientation(0);
  
      }
  
      try{
          mCamera.setPreviewDisplay(sHolder);
          mCamera.startPreview();
      }
      catch(Exception e){
          e.printStackTrace();
      }
  
  }
  
  public void switchflash(){
      //Do the On Flash for now
      if(!flashon){
          mSupportedFlashModes = mCamera.getParameters().getSupportedFlashModes();
          if (mSupportedFlashModes != null &amp;&amp; mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)){
              Camera.Parameters parameters = mCamera.getParameters();
              parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
              mCamera.setParameters(parameters);
          }
      }else{
          //flash on
          //do teh off now
          Camera.Parameters parameters = mCamera.getParameters();
          parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
          mCamera.setParameters(parameters);
      }
      flashon=!flashon;
  
  }
  
  public void stopLoading(){
      loading.dismiss();
      //DialogHelper.showDialog("Oops!", "Your crop had been cancelled !", TouchActivity.reference.getActivity());
  
  }
  </code></pre>