I'm working on implementing the camera into my app. Everything works well except for one thing, when rotating the display from landscape to reversed landscape, the surfaceview is not updated (same when rotated from reversed landscape to normal landscape). surfaceChanged is not called, therefor, the preview shows upside down. If I rotate the display from portrait to either landscape or reversed landscape it works perfectly.
I have tried to catch this using onConfigurationChange in my CameraActivity (which extends ActionBarActivity), but it is not called in the situation where orientation is changed from normal landscape to reversed landscape (or the other way around). I have learned that this is because no config actually changes in this situation. The orientation is still landscape and the screen size doesn't change, thus, onConfigurationChange is not called...
Next, I decided to try and accomplish this using an OrientationEventListener, but I can't seem to access the camera from within this listener:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
// determine our orientation based on sensor response
int lastOrientation = mOrientation;
DisplayMetrics displaymetrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
if (mCamera == null)
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
int angleToRotate = getRoatationAngle();
mCamera.setDisplayOrientation(angleToRotate);
initPreview(width, height);
startPreview();
}
}
};
I keep getting a NullPointerException because mCamera is null whenever onOrientationChanged gets called. I have checked for this null as seen in my code above, but when the call to Camera.open() is invoked, my app crashes. The NullPointerException occurs when trying to set mCamera inside onOrientationChanged using Camera.open().
I've also tried just calling surfaceChanged from within the OrientationEventListener, and the exact same issue occurs. Only the NullPointerException happens when I try to set the null mCamera variable inside surfaceChanged.
Does anyone have an idea how to resolve this issue?