0

The application crashes(Before anything starts there's a dialog that has to be forced closed and then the app exits) at setPreviewDisplay(holder) in SurfaceCreated and I can't figure out why. Please advise. Below is my code.

  public void onCreate(Bundle savedInstanceState) { 
       cameraPreview = new CameraPreview();//CameraPreview has a methos to open the Camera
       cameraObject = CameraPreview.getCameraInstance();
       mHolder = previewSurface.getHolder();//previewSurface is the SurfaceView declared in XML and then I'm doinf findViewById
       mHolder.addCallback(this);

}

   public void surfaceCreated(SurfaceHolder holder) {        

    // The Surface has been created, now tell the camera where to draw the preview.       
   try {           
            cameraObject.setPreviewDisplay(holder);  //CRASHES HERE         
           //cameraObject.startPreview();       
        } catch (IOException e) {   
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());       
        }   
}  

public void surfaceDestroyed(SurfaceHolder holder) {       
    // empty. Take care of releasing the Camera preview in your activity.    
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.       
        // Make sure to stop the preview before resizing or reformatting it.        
        if (mHolder.getSurface() == null){         
            // preview surface does not exist         
                return;        
            }        
        // stop preview before making changes       
        try {            
            cameraObject.stopPreview();      
            } catch (Exception e){         
                   // ignore: tried to stop a non-existent preview     
            }      
            // make any resize, rotate or reformatting changes here     
            // start preview with new settings    
            try {           
                cameraObject.setPreviewDisplay(mHolder);       
                cameraObject.startPreview();        
                } catch (Exception e){      
                       Log.d(TAG, "Error starting camera preview: " + e.getMessage());     
                }   
}

Namratha
  • 16,630
  • 27
  • 90
  • 125

1 Answers1

0

In surfaceCreated, just before setPreviewDisplay, it needs Camera.open and then calling the said method on the camera object. There cannot be any other statement in between these two otherwise it crashes.

Namratha
  • 16,630
  • 27
  • 90
  • 125