0

I am building an app that contains a video capture from a custom camera with a surface view. It works fine in portrait mode, but when I change to landscape mode the camera preview goes to a blank screen.

Here's the code I'm using:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.camera_activity);

        camera1 = Camera.open(camId);
        try {
            camera1.setPreviewDisplay(surfaceHolder);
            camera1.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }

}

In the ConfigurationChanged function I'm not getting the camera1 Object.

How can I solve this?

Tim Malone
  • 3,364
  • 5
  • 37
  • 50
Harsh Bhavsar
  • 1,561
  • 4
  • 21
  • 39
  • 1
    Read [this](https://developer.android.com/guide/topics/resources/runtime-changes.html) on handling orientation changes in Android. – NonCreature0714 Jun 11 '16 at 02:56

1 Answers1

0

You can keep the camera1 object to survive Activity death-and-resurrection, or (less effocient) destroy the camera onStop() or (more efficient) keep the activity instance when configuration changes.

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