get torch (flash) state to toggle it in android M -


i want implement toggle torch functionality. camera not captured , crash - in lollipop - attempting settorchmode in api 23. have code implementation turn on or off torch; however, don't know how check current state of flash.

cameramanager manager = (cameramanager) getsystemservice(context.camera_service); try {     string[] cameras = manager.getcameraidlist();     (int = 0; < cameras.length; i++) {         cameracharacteristics characteristics = manager.getcameracharacteristics(cameras[i]);         boolean flashavailable = characteristics.get(cameracharacteristics.flash_info_available);         if (flashavailable) {             boolean flashstate = getflashstate(); //missing method             manager.settorchmode(cameras[i], !flashstate);          }      }  } catch (cameraaccessexception e) {     e.printstacktrace(); } 

so, how implement getflashstate without using old camera api? , possible without capturing camera?

you can try using

cameramanager.torchcallback torchcallback = new torchcallback() {     @override     public void ontorchmodeunavailable(string cameraid) {         super.ontorchmodeunavailable(cameraid);     }      @override     public void ontorchmodechanged(string cameraid, boolean enabled) {         super.ontorchmodechanged(cameraid, enabled);         flashstate = enabled;     } }; 

and register callback

cameramanager manager = (cameramanager) context.getsystemservice(context.camera_service);     manager.registertorchcallback(torchcallback, null);// (callback, handler) 

when want turn on / off flash uses

manager.settorchmode(cameras[i], !flashstate); 

remember unregister callback...


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -