You cannot use the camera after turning off the flashlight

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()); } } } /* * Turning On flash */ 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; } } /* * Turning Off flash */ 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

+4
source share
2 answers

@Decoid You just need to add a pause method, which I think you donโ€™t have as shown below:

  @Override protected void onPause() { super.onPause(); if (!isFlashOn) { camera.release(); camera = null; } } 
+3
source

release the camera object in pause, make sure you read the Android documentation first.

Documentation for Android !

also read this link

0
source

Source: https://habr.com/ru/post/1485970/


All Articles