使用SurfaceView崩溃的相机应用程序

时间:2011-11-15 06:52:14

标签: android crash surfaceview android-camera surfaceholder

在SurfaceCreated的setPreviewDisplay(holder)中,应用程序崩溃(在任何事情开始之前有一个必须强制关闭然后应用程序退出的对话框),我无法弄清楚原因。 请指教。以下是我的代码。

  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());     
                }   
}

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()); } }

1 个答案:

答案 0 :(得分:0)

在surfaceCreated中,就在setPreviewDisplay之前,它需要Camera.open然后在相机对象上调用所述方法。 在这两者之间不能有任何其他陈述,否则它会崩溃。