title: Android Surface Camera Preview crash after onPause/onBackPressed
body: <p>Im trying to set up a custom SurfaceView (which work as excepted) for the first time.</p>
  
  <p>After onResume/onPause has been called im getting a "media server died" message. </p>
  
  <p>After the media server died i can add another preview. Any idea why it's crashing? Im Releasing the Camera in onPause and open the camera on onResume.</p>
  
  <p>-- Camera Activity --
      public class CameraActivity extends DashboardActivity {</p>
  
  <pre><code>private static String TAG = "CameraActivity";
  
  Camera camera;
  CameraPreview cameraPreview;
  Context _context;
  FrameLayout frameLayout;
  CameraPreview preview;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.camera_layout);
  
      this._context = this;
      frameLayout = (FrameLayout) findViewById(R.id.preview);
  
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      camera = Camera.open();
  
      if (null != camera) {
          camera.setDisplayOrientation(90);
          preview = new CameraPreview(getApplicationContext(), camera);
          frameLayout.addView(preview);
      }
  
  }
  
  @Override
  public void onBackPressed() {
      super.onBackPressed();
      if (camera != null) {
          Log.e(TAG, "Camera was not null on back pressed!");
          try {
              camera.stopPreview();
          } catch (Exception e) {
          }
          // camera.release();
          // camera = null;
      }
      finish();
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      if (camera != null) {
          camera.release();
          camera = null;
      }
      frameLayout.removeView(preview);
      preview = null;
  }
  </code></pre>
  
  <p>-- Camera Preview --</p>
  
  <pre><code>public class CameraPreview extends SurfaceView implements
      SurfaceHolder.Callback {
  private static final String TAG = "Preview";
  
  SurfaceHolder mHolder;
  public Camera camera;
  
  CameraPreview(Context context, Camera camera) {
      super(context);
  
      this.camera = camera;
      // Install a SurfaceHolder.Callback so we get notified when the
      // underlying surface is created and destroyed.
      mHolder = getHolder();
      mHolder.addCallback(this);
      mHolder.setFormat(PixelFormat.RGB_332);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
  }
  
  public void surfaceCreated(SurfaceHolder holder) {
      try {
          Camera.Parameters p = camera.getParameters();
          p.setPictureSize(80, 60);
          p.setColorEffect(android.hardware.Camera.Parameters.EFFECT_NONE);
          p.setJpegQuality(20);
          p.setPreviewFrameRate(1);
          p.setPreviewFpsRange(5, 10);
          p.setPreviewSize(80, 60);
          camera.setParameters(p);
          try {
              camera.setPreviewDisplay(holder);
  
              camera.setPreviewCallback(new PreviewCallback() {
  
                  public void onPreviewFrame(byte[] data, Camera arg1) {
                  }
              });
          } catch (IOException e) {
              e.printStackTrace();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public void stopCameraPreview() {
  
  }
  
  public void takePicture() {
      camera.takePicture(null, null, null);
      stopCameraPreview();
  
  }
  
  public void surfaceDestroyed(SurfaceHolder holder) {
      Log.e(TAG, "SurfaceDestroyed called");
      if (camera != null) {
          try {
              camera.stopPreview();
          } catch (Exception e) {
          }
          // camera.lock();
          // camera.release();
          // camera = null;
      }
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  
      Log.d(TAG, "SurfaceChanged called!");
  
      if (holder.getSurface() == null) {
          Log.d(TAG, "SurfaceChanged Surface is null! Stopping!");
          return;
      }
  
      try {
          Camera.Parameters parameters = camera.getParameters();
          // parameters.setPreviewSize(80, 60);
          camera.setParameters(parameters);
          // camera.setPreviewDisplay(holder);
  
          camera.startPreview();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>