I have one activity class(CameraActivity) which is using my CameraPreview class. In \"OnResume\" the camera and preview are initiated. In \"OnPause\", i am releasing camera
Dan is right. Also see here.
code sample:
public class MainActivity extends Activity {
private FrameLayout mFlCameraPreview;
private Camera mCamera;
private CameraPreview mCameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFlCameraPreview = (FrameLayout) findViewById(R.id.main_fl_camera_preview);
}
@Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
mCamera = getCameraInstance();
}
if (mCameraPreview == null) {
mCameraPreview = new CameraPreview(this, mCamera);
mFlCameraPreview.addView(mCameraPreview);
}
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
if (mCameraPreview != null) {
mFlCameraPreview.removeView(mCameraPreview);
mCameraPreview = null;
}
}
public static Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
}
return camera;
}
}