0

I have also launched the app in the market. I got an error message from one of the users that he is getting an error while opening the camera.

i have the same problem

so i try to replace this code :

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

to this code :

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

i got NullPointerException error

Even I do not care about display change becouse it only landscape

android:screenOrientation="landscape"
Community
  • 1
  • 1
idan
  • 1,508
  • 5
  • 29
  • 60

1 Answers1

1

In the linked article the exception was RuntimeException caused by trying to set an unsupported preview size. But in your case it's a NullException. Probably, your mCamera object was not initialized.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307