I am writing an application for school, and it has functions that require the camera to take pictures, crop them, upload them to the server, and then the user had to turn on the flashlight (as part of some tracking they did). All of the above works fine - until the user turns off the flashlight and wants to make another image. The camera stops working, an error message appears on the screen that it cannot connect to the camera.
I know that there are commands like reconnect: http://developer.android.com/reference/android/hardware/Camera.html#reconnect () I messed with him and I canโt get it to work in my life.
To use the camera, I use the intention:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
And the flashlight code is as follows:
private void getCamera() { if (camera == null) { try { camera = Camera.open(); params = camera.getParameters(); } catch (RuntimeException e) { Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); } } } private void turnOnFlash() { getCamera(); if (!isFlashOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isFlashOn = true; } } private void turnOffFlash() { if (isFlashOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); isFlashOn = false; } }
Is there a way that I can use the same camera instance for both the flashlight and the camera (when this is intentional)? I found that if I let go of the camera, it would become zero and she would not be able to initiate it again.
Thank you in advance
source share