致命异常:java.lang.RuntimeException在调用Camera.release()之后使用了Camera

时间:2018-11-30 11:16:38

标签: java android android-camera

我是android的再见者,因此遇到了这个camera.release()问题。 这种崩溃在三星设备中经常发生

Fabric crashlytics:

approvementcheck.CameraPreview.surfaceChanged(CameraPreview.java:147)

enter image description here

 public class CameraPreview extends SurfaceView implements 
 SurfaceHolder.Callback {
 private SurfaceHolder mHolder;
 private Camera mCamera;
 private CameraPreview cameraPreview;
 private float oldDist = 1f;
 public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
 private SurfaceHolder mHolder;
 private Camera mCamera;
 private CameraPreview cameraPreview;
 private float oldDist = 1f;

 public CameraPreview(Context context, Camera camera) {
     super(context);
     mCamera = camera;

     mHolder = getHolder();
     mHolder.addCallback(this);

 }

 public void surfaceCreated(SurfaceHolder holder) {
     // The Surface has been created, now tell the camera where to draw the preview.
     try {
         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();
     } catch (Exception e) {
         WebHelper.Pass(e);
     }
 }

public void setmHolder() {
    try {
        mCamera.setPreviewDisplay(mHolder);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 public void setCamera(Camera camera) {
     mCamera = camera;
     //Set autoFocus
     Camera.Parameters params = mCamera.getParameters();

     try {
         mCamera.setParameters(setPictureSize(params));
     } catch (Exception e) {
         WebHelper.Pass(e);
     }

     try {
         mCamera.setPreviewDisplay(mHolder);
         mCamera.startPreview();

     } catch (Exception e) {
         WebHelper.Pass(e);
     }
 }

 public void setFlashMode(int mode) {

     String flashMode = Camera.Parameters.FLASH_MODE_OFF;
     switch (mode) {
         case 1:
             flashMode = Camera.Parameters.FLASH_MODE_OFF;
             break;
         case 2:
             flashMode = Camera.Parameters.FLASH_MODE_AUTO;
             break;
         case 3:
             flashMode = Camera.Parameters.FLASH_MODE_ON;
             break;
     }


     Camera.Parameters params = mCamera.getParameters();
     params.setFlashMode(flashMode);

     try {
         mCamera.setParameters(params);
     } catch (Exception e) {
         WebHelper.Pass(e);
     }
 }

 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 || mCamera == null) {
         // preview surface does not exist
         return;
     }

     // stop preview before making changes
     try {
         mCamera.stopPreview();
     } catch (Exception e) {
         WebHelper.Pass(e);
     }

     Camera.Parameters params = mCamera.getParameters();


     try {
         mCamera.setParameters(setPictureSize(params));
     } catch (Exception e) {

         WebHelper.Pass(e);
     }


     // start preview with new settings
     try {
         mCamera.setPreviewDisplay(mHolder);
         mCamera.startPreview();

     } catch (Exception e) {
         WebHelper.Pass(e);//check
     }

     }//line 147 shows here



 public void startCamera() {
     try {
         mCamera.setPreviewDisplay(mHolder);
         mCamera.startPreview();
     } catch (Exception e) {
         WebHelper.Pass(e);
     }
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
     stopCamera();
 }

 public void stopCamera() {
     try {

         mCamera.stopPreview();
         mCamera.release();
     } catch (Exception e) {
         WebHelper.Pass(e);
     }
 }

 private boolean checkFocusMode() {
     Camera.Parameters params = mCamera.getParameters();

     List<String> focusModes = params.getSupportedFocusModes();
     return focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO);
 }

CameraActivity 类:

 onCreate{
 ..
 mCamera = getCameraInstance(0);
 ..
 }

 public static Camera getCameraInstance(int id) {
    Camera c = null;
    try {
        c = Camera.open(id); // attempt to get a Camera instance
        currentCameraId = id;
    } catch (Exception e) {
        WebHelper.Pass(e);

    }
    return c; // returns null if camera is unavailable
    }
    ...
    case R.id.changeCameraButton:
    if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
    } else {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
    }
    mCamera.release();
    mCamera = null;
    mCamera = getCameraInstance(currentCameraId);
    mPreview.setCamera(mCamera);
    mPreview.setmHolder();
    mCamera.startPreview();
    break;
   }

我会很感谢所有建议,这些建议可以引导我解决这个问题

0 个答案:

没有答案