Camera locking while testing code


#1

Hi,

Listing 19.2 provides the following code to takePictureButton’s OnClickListener:

takePictureButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getActivity().finish();
				
			}
		});

Since the code presented on chapter 19 doesn’t manage taking and saving pictures, when you click on Take! button placed on fragment_crime_camera.xml, you close the activity without release the camera. This causes Criminal Intent to close on any time you try to run the app after the first time you run it, until you reboot the device (as you pointed in the same chapter, while discussing camera lifecycle methods).

While this problem is solved on chapter 20, as far as I’m concerned, I suggest you put some code to release the camera as temporary implementation to takePictureButton’s OnClickListener.

I’d put the following code on listener:

takePictureButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mCamera != null) {
					mCamera.release();
					mCamera = null;
				}
				getActivity().finish();
				
			}
		});

Is there any implications to this code that prevented you to put it on the book?


#2

The code in Listing 19.6 will release the camera after finish() is called.


#3

Hi,

I forgot to write down onPause() method. Sorry!

Thanks! :slight_smile: