Flash light not detected
Asked Answered
C

4

7

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

Crampon answered 7/7, 2013 at 17:40 Comment(3)
have you tried to put these lines cam = Camera.open(); param = cam.getParameters(); before your first if() statement on the onCreate()?Culdesac
Have you tested this in all device?Buskined
Please test @Ilya_Gazman's code on a XOLO A600 or Moto G...Lindi
G
2

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!

Guajardo answered 30/6, 2014 at 19:33 Comment(0)
B
0

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.

Bradfordbradlee answered 25/6, 2014 at 15:45 Comment(1)
Ok i see now, on which device are you testing this? Do you have the same problem with other devices?Bradfordbradlee
C
0

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.

Coad answered 30/6, 2014 at 9:51 Comment(0)
C
0

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);
Cide answered 2/7, 2014 at 15:10 Comment(4)
@Lindi No, I just made it up to see my icon on the screenCide
hehe sooooo funny! :PLindi
I am desperate with this problem, now my problem is my app is working on all my devices, I don't have a device that doesn't work with the old code... I found this issue on XOLO A600Lindi
Is this happening with Moto G?Lindi

© 2022 - 2024 — McMap. All rights reserved.