@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.switch_cam: // Release this camera -> mCameraCurrentlyLocked if (mCamera != null) { mCamera.stopPreview(); mPreview.setCamera(null); mCamera.release(); mCamera = null; } // Acquire the next camera and request Preview to reconfigure // parameters. mCamera = Camera.open((mCameraCurrentlyLocked + 1) % mNumberOfCameras); mCameraCurrentlyLocked = (mCameraCurrentlyLocked + 1) % mNumberOfCameras; mPreview.switchCamera(mCamera); // Start the preview mCamera.startPreview(); return true; case android.R.id.home: Intent intent = new Intent(this.getActivity(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); default: return super.onOptionsItemSelected(item); } }
@Override public void onResume() { super.onResume(); // Open the default i.e. the first rear facing camera. mCamera = Camera.open(mDefaultCameraId); mCameraCurrentlyLocked = mDefaultCameraId; mPreview.setCamera(mCamera); }
@Override public void onPause() { super.onPause(); // Because the Camera object is a shared resource, it's very // important to release it when the activity is paused. if (mCamera != null) { mPreview.setCamera(null); mCamera.release(); mCamera = null; } }