android camera.setParameters失败了

时间:2013-06-02 13:45:09

标签: android android-camera

我也在市场上推出了该应用程序。我收到其中一个用户的错误消息,说他在打开相机时出错了。

i have the same problem

所以我尝试替换这段代码:

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.



         Log.d(TAG, "surfaceChanged to " + "," + w + "," +  h);

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

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

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

到此代码:

   public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
  //      List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

        // You need to choose the most appropriate previewSize for your app
        Camera.Size previewSize = parameters.getPreferredPreviewSizeForVideo();   // .... select one of previewSizes here

        parameters.setPreviewSize(previewSize.width, previewSize.height);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

我收到了NullPointerException错误

即使我不关心显示更改因为它只是风景

android:screenOrientation="landscape"

1 个答案:

答案 0 :(得分:1)

在链接文章中,尝试设置不受支持的预览尺寸导致异常RuntimeException。但在你的情况下,它是NullException。可能是您的mCamera对象未初始化。