title: Camera API working example?
body: <p>It's my first use with the Camera dev on Android and I have tested the CameraDemo that I have find at the link <a href="https://thenewcircle.com/s/post/39/using__the_camera_api" rel="nofollow">https://thenewcircle.com/s/post/39/using__the_camera_api</a></p>
  
  <p>But I have somes problems with this code :(</p>
  
  <p>First, this seem necessary to add this line into the code of Preview(Context context)   </p>
  
  <pre><code>if(Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.HONEYCOMB)  
  </code></pre>
  
  <p>(the program crash if I don't add this test)  </p>
  
  <p>just before this line   </p>
  
  <pre><code>mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  </code></pre>
  
  <p>because the setType() call seem to be now deprecated
  (that is what say to me Android Studio)</p>
  
  <p>Secondly, this seem necessary to comment the camera.setParameters() call into the SurfaceChanged(SurfaceHolder holder, int format, int w, int h) code  </p>
  
  <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      // Now that the size is known, set up the camera parameters and begin
      // the preview.
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPreviewSize(w, h);
      // camera.setParameters(parameters);
      camera.startPreview();
  </code></pre>
  
  <p>(it crash if this line is uncommented)</p>
  
  <p>Now, this example don't crash at startup on my Android devices and I can see the camera preview :)  </p>
  
  <p>But it crash now when I rotate the screen :(  </p>
  
  <p>=> what is the method for that to handle the screen rotation during the camera preview ?<br>
  (since camera.setParameters(parameters) seem deprecated ...)  </p>
  
  <p>EDIT : this is now partially resolved :)</p>
  
  <p>I have make this little transformation : </p>
  
  <ul>
  <li><p>add a boolean variable mPreviewRunning that is is initialised to false at the beginning</p></li>
  <li><p>add a camera.stopPreview() call when this variable is already set to true</p></li>
  <li><p>set this variable to true just after the call of camera.startPreview();</p></li>
  </ul>
  
  <p><code></p>
  
  <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)  
  {  
      // Now that the size is known, set up the camera parameters and begin  
      // the preview  
      // ERROR : setParameter() is deprecated  
      // Camera.Parameters parameters = camera.getParameters();  
      // parameters.setPreviewSize(w, h);   
      // camera.setParameters(parameters);  
      if( bPreviewRunning == true)  
      {  
          bPreviewRunning = false;  
          camera.stopPreview();  
      }  
      camera.startPreview();
      bPreviewRunning = true;  
  }
  </code></pre>
  
  <p></code></p>
  
  <p>But the application crash hovewer sometimes but I don't understand why :(<br>
  (and the preview is on the bad orientation when in portrait mode)</p>