java - How to open device flashlight in Android N? -


i'm trying make simple button turn on/off device flashlight. don't understand why android.hardware.camera obsolete. have in order make code working on devices , ones older version of android?

this code:

if (isflashlighton) {   if (getpackagemanager().hassystemfeature(         packagemanager.feature_camera_flash))    {     cam = camera.open();     camera.parameters p = cam.getparameters();     p.setflashmode(camera.parameters.flash_mode_torch);     cam.setparameters(p);     cam.startpreview();   }   else   {     try     {       cam.stoppreview();       cam.release();       cam = null;     }     catch (exception ex)     {       // ignore exception     }   } } 

there mistake in logic of code. it's not related specific android version. checking if device has camera flashlight , then, turns on. in else block turning camera flashlight off in case when device has no camera flashlight what never happen if device has flashlight.

i think code should below. toggle flashlight (turn on, when it's turned off , turn off when it's turned on).

boolean isflashlighton = false; boolean devicehascameraflash = getpackagemanager().hassystemfeature(                                  packagemanager.feature_camera_flash);  if(devicehascameraflash) {   camera camera = camera.open();   camera.parameters parameters = camera.getparameters();    if(isflashlighton) {      // turn flashlight off     parameters.setflashmode(parameters.flash_mode_off);     camera.setparameters(parameters);     camera.stoppreview();     isflashlighton = false;   } else {     // turn flashlight on     parameters.setflashmode(camera.parameters.flash_mode_torch);     camera.setparameters(parameters);     camera.startpreview();     isflashlighton = true;   } } 

i couldn't test code right now, think should work, should general idea , adjust purposes.

to avoid warnings in ide , static code analysis tools, need add @suppresswarnings("deprecation") annotation deprecated code. need keep in order have backward compatibility older android versions.

if want handle camera on both new , old android versions, should prepare separate code these versions.

according documentation:

we recommend using new android.hardware.camera2 api new applications.

it means should in following way:

if (build.version.sdk_int >= build.version_codes.lollipop) {   // code lollipop devices or newer } else {   // code pre-lollipop devices } 

code toggling flashlight new api follows:

private void toggleflashlight(boolean isflashlighton) {   cameramanager cammanager = (cameramanager) getsystemservice(context.camera_service);   string cameraid = cammanager.getcameraidlist()[0]; // front camera @ 0 position.   cammanager.settorchmode(cameraid, isflashlighton); } 

moreover, check out these stackoverflow threads:

they may helpful while dealing issue.

regards


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -