How to free the camera when people stop using your widget?

I built a widget that uses a camera for his flashlight. It works great on and off, and frees it up when people actually turn it off, but if I turn it on and then I go to the camera, it certainly crashes the camera application, how can I release the camera if the user forgot to turn off the flashlight? Is there any way to detect when a new application will be launched before it calls the camera?

+5
source share
2 answers

Based on my research, this is not possible on standard Android devices, Samsung has achieved this with one of its applications, but this is most likely due to their improvement in the basic functions of Android. This will mean, however, that they will need to issue documentation on how this will be achieved, and this will only work on the phones on which they turned it on.

If someone has an answer that is really useful, and then clarifies why this cannot be done, I would be happy to accept it, I will leave it here, hoping that other people see it and do not waste time researching the subject until those since I did it.

* Edit if Google updates the OS in some way so that applications can check if the camera is being used, and if so, to free it, then it will work easily, but, alas, we have to wait for them. Or design on an iPhone where you don't need to contact the camera to access the flash.

+2
source

Put the code below in your onDestroy() method of your activity:

 protected void onDestroy(){ if(camera!=null){ camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; } } 

If you are using a separate preview class, add the code below:

 public void surfaceDestroyed(SurfaceHolder holder) { if(camera!=null){ camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; } } 
+1
source

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


All Articles