I have absolutely no idea how to implement this. It works fine on my Razr, but fails on a lot of Samsung devices. I have a SurfaceView running a camera preview. I've included a button to use the flashlight. The exception is thrown where I inserted the try block:
if (hasFlash()) {
btnLight = (ImageButton) findViewById(R.id.btn_light);
btnLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Parameters p = camera.getParameters();
if (p.getSupportedFlashModes() != null) {
if (p.getSupportedFlashModes().contains(
Parameters.FLASH_MODE_TORCH)) {
if (isLightOn) {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
btnLight.setImageResource(R.drawable.light_on);
isLightOn = false;
} else {
try {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
btnLight.setImageResource(R.drawable.light_off);
isLightOn = true;
} catch (RuntimeException e){
// I don't know how to make this stupid thing work for all phones
}
}
}
}
}
});