I'm trying to turn on the flash of the cell phone as a flashlight, from an item in the action bar, but I can not turn it on, the code is e as follows:
Variable delcara at the start of the calse to manage the on and off:
boolean isOn = false;
Menu options:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.btnOtro) {
}
if(id== R.id.btnFlash) {
if(isOn==false)
isOn=true;
else{
isOn=false;
}
flash();
}
return super.onOptionsItemSelected(item);
}
And this is the method to start the flash:
public void flash()// metodo para activar el flash
{
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
camera= Camera.open();
parameters = camera.getParameters();
}
if(isOn==true)
{
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
else
{
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
}
}
If someone solved something like that, it would be very useful for me to share, Thank you.