I am trying to write an app which turns on flash light when a button is pressed. The problem is the app is not detecting flash light on my phone. I have searched alot on internet. Sure others have faced the problem, I have also applied those solutions but they don't seem to work. I don't know what is causing this problem. Posting the code here:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if(! getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) // checking if flash light is available inn android phone
{
Toast.makeText(StartingPoint.this, "Sorry this app can't work without flash light", Toast.LENGTH_LONG).show();
finish();
}
cam = Camera.open();
param = cam.getParameters();
}
@Override
public void onClick (View v)
{
if(!flashOn)
{
i=0;
flashOn=true;
param.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(param);
cam.startPreview();
}
else{
i=0;
flashOn=false;
param.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(param);
cam.stopPreview();
}
}
I have added these permissions in Android Manifest as well.
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Regards