Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash light not detected

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

like image 716
user2498079 Avatar asked Jul 07 '13 17:07

user2498079


1 Answers

I have an app that checks the flashlight feature and it works fine. Here is the code I used for checking if the user has the light:

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    new AlertDialog.Builder(this)
    .setTitle("Sorry")
    .setMessage("It appears that your device is incompatible with this app. Sorry for the inconvenience.")
    .setNeutralButton("Close", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            finish();
        }
    }).show();
    return;
}

Now to actually make the light work, I made a toggle button and wrote the following code:

private boolean isLightOn = false;
private Camera camera;
private ToggleButton button;
public Vibrator v;

if (camera == null) {
    camera = Camera.open();
}

final Parameters p = camera.getParameters();

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if (isLightOn) {

        Toast.makeText(context, "Light off!", Toast.LENGTH_SHORT).show();
        v.vibrate(40);
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        isLightOn = false;

        } else {

            Toast.makeText(context, "Light on!", Toast.LENGTH_SHORT).show();
        v.vibrate(40);
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        camera.startPreview();
        isLightOn = true;

        }

    }
});

And finally, here are the only permissions I used:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Note: All of the above code is in the onCreate method of my activity.

Hope this helps solve your problem!

like image 108
Andrew Quebe Avatar answered Sep 20 '22 05:09

Andrew Quebe