Camera Flash not working in Android -


i calling following "action method" on button click me wrong when click on button flash doesn't work.

private void  actionflash(){  /* first check if device supporting flashlight or not */     hasflash = this.getapplicationcontext().getpackagemanager().hassystemfeature(packagemanager.feature_camera_flash);     if(!hasflash){         // device doesn't support flash         // show alert message , close application         alertdialog alert = new alertdialog.builder(this).create();         alert.setmessage("sorry, device doesn't support flash light!");         alert.setbutton(dialoginterface.button_positive, "ok", new dialoginterface.onclicklistener(){             public void onclick(dialoginterface dialog, int which){                 dialog.dismiss();             }         });         alert.show();         return;     }     else{           cameramanager mcameramanager = (cameramanager) this.getsystemservice(context.camera_service);         try{             string mcameraid = mcameramanager.getcameraidlist()[0];             if(mcameraid.equals("1")) {    // on camera                 if(!isflashon){  // if flash light off                     // turn on flash light                     try{                         if(build.version.sdk_int >= build.version_codes.m){                             mcameramanager.settorchmode(mcameraid, true);                         }                     }catch(exception e){                         e.printstacktrace();                     }                     // change isflashon boolean value                     isflashon = true;                     // change button icon                     flash.setimageresource(r.drawable.ic_flash_off_black_24dp);                  }else{ // if flash light on                     // turn off flash light                     try{                         if(build.version.sdk_int >= build.version_codes.m){                             mcameramanager.settorchmode(mcameraid, false);                         }                     }catch(exception e){                         e.printstacktrace();                     }                     // change isflashon boolean value                     isflashon = false;                     // change button icon                     flash.setimageresource(r.drawable.ic_flash_on_black_24dp);                 }             }         }catch(cameraaccessexception e){             toast.maketext(this, "cannot access camera.", toast.length_short).show();             this.finish();         }     } } 

i using following setup camera code flash button doesn't responds:

private void setupcamera(int width, int height) {     cameramanager cameramanager = (cameramanager) getsystemservice(context.camera_service);     try {         for(string cameraid : cameramanager.getcameraidlist()){             cameracharacteristics cameracharacteristics = cameramanager.getcameracharacteristics(cameraid);             if(cameracharacteristics.get(cameracharacteristics.lens_facing) ==                     cameracharacteristics.lens_facing_front) {                 continue;                 }                 streamconfigurationmap map = cameracharacteristics.get(cameracharacteristics.scaler_stream_configuration_map);                 int deviceorientation = getwindowmanager().getdefaultdisplay().getrotation();                 mtotalrotation = sensortodevicerotation(cameracharacteristics, deviceorientation);                 boolean swaprotation = mtotalrotation == 90 || mtotalrotation == 270;                 int rotatedwidth = width;                 int rotatedheight = height;                 if(swaprotation) {                     rotatedwidth = height;                     rotatedheight = width;                 }                 mpreviewsize = chooseoptimalsize(map.getoutputsizes(surfacetexture.class), rotatedwidth, rotatedheight);                 mvideosize = chooseoptimalsize(map.getoutputsizes(mediarecorder.class), rotatedwidth, rotatedheight);                 mimagesize = chooseoptimalsize(map.getoutputsizes(imageformat.jpeg), rotatedwidth, rotatedheight);                 mimagereader = imagereader.newinstance(mimagesize.getwidth(), mimagesize.getheight(), imageformat.jpeg, 1);                 mimagereader.setonimageavailablelistener(monimageavailablelistener, mbackgroundhandler);                 mcameraid = cameraid;                 return;             }     } catch (cameraaccessexception e) {         e.printstacktrace();     } } 


Comments