为什么android.harware.camera会在某些设备上抛出错误?

时间:2016-12-10 07:07:40

标签: java android android-layout android-studio camera

我使用SurfaceHolder创建了一个相机应用,在我的主要设备Samsung Galaxy J5上进行了测试,一切看起来很好。

但是当我在我的辅助设备S5 Neo上测试它时,它崩溃并显示如下错误:

  

setParameters失败

以下是错误代码:

public void onClick(View v) {

    camera.stopPreview();
    camera.release();
    camera = null;

    try {
        releaseCameraAndPreview();
        camera = camera.open(1);
    }
    catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    android.hardware.Camera.Parameters parameters;
    camera.setFaceDetectionListener(faceDetectionListener);
    camera.startFaceDetection();
    parameters = camera.getParameters();
    parameters.setPreviewFrameRate(20);
    List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();

    Camera.Size customSize = customSizes.get(0); //Added size
    parameters.setPreviewSize(customSize.width, customSize.height);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    camera.setParameters(parameters);
    camera.setDisplayOrientation(90);

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();

    }
    catch (Exception e) {

    }
}

private void releaseCameraAndPreview() {
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

(顺便说一下,它给了我同样的错误,但是ColorEffect.BLACKBOARD。)

然后我在我的很多朋友的设备上测试了它,比如 LG精神 Nexus 5(AVD),它发出错误:

  

致命异常:java.lang.NullPointerException尝试调用虚方法&#39; android.hardware.Camera $参数android.hardware.Camera.getParameters()&#39;在空对象引用上

结果:它甚至无法启动应用程序而不会崩溃......

以下是该错误的代码:

 public void surfaceCreated(SurfaceHolder surfaceHolder ) {

    try {
        releaseCameraAndPreview(); //releaseCamera... is on the earlier code
            camera = camera.open();
    }
    catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    }
    catch (Exception e) {

    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    refreshCamera();
    setDisplayOrientation();

    Camera.Parameters parameters = camera.getParameters();

    camera.setFaceDetectionListener(faceDetectionListener);
    camera.startFaceDetection();
    parameters.setPreviewFrameRate(20);
    List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
    Camera.Size customSize = customSizes.get(0); //Added size
    parameters.setPreviewSize(customSize.width, customSize.height);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    camera.setParameters(parameters);
    camera.setDisplayOrientation(90);

    camera.setParameters(parameters);
    camera.startPreview();
}

0 个答案:

没有答案