user2498079
user2498079

Reputation: 3012

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

Upvotes: 7

Views: 721

Answers (4)

Ilya Gazman
Ilya Gazman

Reputation: 32231

Some cameras need surface holder, otherwise they block the flash.

SurfaceView preview = (SurfaceView) findViewById(...);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
Camera camera = Camera.open();
camera.setPreviewDisplay(holder);

Upvotes: 0

Andrew Quebe
Andrew Quebe

Reputation: 2293

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!

Upvotes: 2

Samjack
Samjack

Reputation: 188

I had the same problem. Use this

if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
  //Flash ok
  Parameters params = mCamera.getParameters();
  params.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
  //Flash not supported
}

to determinate if your device has flash.

Upvotes: 0

Francesco verheye
Francesco verheye

Reputation: 1574

I think you aren't setting your params again: I used this to check if there is a flashlight:

public static Boolean hasFlashLight(Context context){
    return context.getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}

and to turn it off and on:

Parameters params = mCamera.getParameters();
if (!isFlashlightOn) {
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
} else {
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);

Let me know if it works for you too.

Upvotes: 0

Related Questions