At last I succeeded to find out the problem of my code and got the solution. When i try to reopen the camera in my onResume()
method I missed a part. When I create/open a new camera in onResume()
, the FrameLayout still has my previous camera. So All I do is remove my previous camera from FrameLayout on onPause()
method and then recreate everything on onResume() method. That solve my problem and now it is working nicely without any error. Here is my onPause()
and onResume()
method with the fix. Hope this might help somebody in future.
public void onPause() {
super.onPause();
releaseCamera();
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.removeView(mPreview);
}
public void onResume(){
super.onResume();
try {
if(mCamera==null){
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
this.getWindowManager().getDefaultDisplay().getRotation();
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}
private void releaseCamera() {
//Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release();
mCamera = null;
mPreview= null;
}
}
Thanks, Sakib