android - Flashlight control in Marshmallow -
i have problem regarding camera in recent marshmallow build, more flashlight. on pre-marshmallow version need turn flash on/off following:
private void turnflashon(final camera camera, int flashlightdurationms) { if (!isflashon()) { final list<string> supportedflashmodes = camera.getparameters().getsupportedflashmodes(); if (supportedflashmodes != null && supportedflashmodes.contains(camera.parameters.flash_mode_torch)) { mparams.setflashmode(camera.parameters.flash_mode_torch); camera.setparameters(mparams); } } }
and
private void turnflashoff(camera camera) { if (camera != null) { final list<string> supportedflashmodes = camera.getparameters().getsupportedflashmodes(); if (supportedflashmodes != null && supportedflashmodes.contains(camera.parameters.flash_mode_off)) { mparams.setflashmode(camera.parameters.flash_mode_off); camera.setparameters(mparams); } } }
unfortunately, marshmallow devices began crash in wild. somehow camera.getparameters()
, camera.setparameters()
began fail messages such as:
runtimeexception: getparameters failed (empty parameters)
runtimeexception: setparameters failed
i tried starting , stopping preview before getting parameters, no longer throws errors. preview not resumed when call camera.startpreview()
.
i fear releasing camera , reopening out of question takes seconds , produce bad experience.
any suggestions on how turn flashlight on/off in marshmallow reliably?
google has introduced torchmode in os 6 (android m).
if purpose turn on/off flash, below code can that:
private static void handleactionturnonflashlight(context context){ try{ cameramanager manager = (cameramanager) context.getsystemservice(context.camera_service); string[] list = manager.getcameraidlist(); manager.settorchmode(list[0], true); } catch (cameraaccessexception cae){ log.e(tag, cae.getmessage()); cae.printstacktrace(); } } private static void handleactionturnoffflashlight(context context){ try{ cameramanager manager = (cameramanager) context.getsystemservice(context.camera_service); manager.settorchmode(manager.getcameraidlist()[0], false); } catch (cameraaccessexception cae){ log.e(tag, cae.getmessage()); cae.printstacktrace(); } }
all have is: cameraid's list out of camera id zero(0) primary camera want turn flash on/off. pass cameraid settochmode api boolean value turning on or off.
do note piece of code work os 6, need check device os , based upon need select api's call pre-marshmallow devices.
kindly mark solution if solves problem.
Comments
Post a Comment