How to open flashLight of android phone programmatically
declare these variables in the activity
private boolean isLighOn = false; // to check tht flash is on or off
boolean flashAvailable=false;
int cameraID=0 /*cameraId with 0 represents back camera**/
firstly we have to check if phone supports flash light or not.
Camera myCamera=getCameraInstance(cameraID);
final Parameters parameter = myCamera.getParameters();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
flashAvailable=true;
}
else
{
flashAvailable=false;
}
code snippet to on-off the falsh
Button button = (Button) findViewById(R.id.buttonFlashlight);
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) {
if(flashAvailable){
if (isLighOn)
{
p.setFlashMode(Parameters.FLASH_MODE_OFF);
try {
myCamera.setParameters(p);
}
catch(RuntimeException ex)
{
}
myCamera.startPreview();
isLighOn = false;
}
else
{
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
try {
myCamera.setParameters(p);
} catch(RuntimeException ex)
{
} myCamera.startPreview();
isLighOn = true;
}
}
else
{
Toast.makeText(getApplicationContext(),"Your Phone does not have flash", Toast.LENGTH_SHORT).show();
}
}
});
0 Comment(s)