Camera.Parameters.FLASH_MODE_TORCH replacement for Android 2.1
Asked Answered
M

2

7

I am trying to write an app that requires the LED flash to go into torch mode. The problem is, Android 2.1 does not support this mode and therefore I cannot support the platform yet. Wouldn't be an issue, but I am writing it for my fiance and her Epic 4G only has 2.1 right now. I found some code samples that use some undocumented API calls and therefore work on the Motorola Droid and such, but they do not work on the Epic. Does anyone have some suggestions on where to look to find code that should help me get this working?

Medan answered 7/10, 2010 at 2:38 Comment(0)
L
9

I'm finding that torch mode is generally working fine on 2.1 but I had the same problem with the Samsung Epic and found a hack around it.

Looking at the params returned by Camera.getParameters() when run on the Samsung Epic, I noticed that the flash-modes it claims to support are: flash-mode-values=off,on,auto;

torch-mode is not listed, implying it's not supported.

However, I found that this model would still accept that mode and WOULD turn the LED on! The bad news was that when later setting the flash-mode back to auto or off left the LED still lit! It will not turn off until you call Camera.release().

I guess that's why Samsung dont include it in the list of supported!?!

So...the method I use to toggle torch in a CameraHelper class is...

/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */
public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_AUTO;
    }

    try{    
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
        MyLog.e(mLOG_TAG, this.getClass().getSimpleName() +  " error setting flash mode to: "+ value + " " + ex.toString());
    }
}

The activities that use this call it as follows...

private void toggleFlashLight()
{
    mIsFlashlightOn = ! mIsFlashlightOn;

    /**
     * hack to fix an issue where the Samsung Galaxy will turn torch on,
     * even though it says it doesnt support torch mode,
     * but then will NOT turn it off via this param.
     */
    if (! mIsFlashlightOn && Build.MANUFACTURER.equalsIgnoreCase("Samsung"))
    {
        this.releaseCameraResources();
        this.initCamera();
    }
    else
    {
        boolean result = mCamHelper.setFlashlight(mIsFlashlightOn);
        if (! result)
        {
            alertFlashlightNotSupported();
        }
    }
}

The magic that makes this work in releaseCameraResources() is that it calls Camera.release()....and then I have to reinitialize all my camera stuff for Samsung devices.

Not pretty but seems to be working for plenty of users.

Note that I do have a report of torch mode not working at all with this code on Nexus one but have been able to dig into it. It definitely works on HTC EVO and Samsung Epic.

Hope this helps.

Laruelarum answered 15/10, 2010 at 5:14 Comment(7)
Not sure if I did something wrong, but I'm getting a Force Close when I try to debug it.Medan
No offence, but basing functionality on the phone maker is not the ideal way to go. Who knows what functionality SAMSUNG phones will have in half a year or more for instance?Jeana
I have used your code. But the camera flashlight is automatically getting turned off after few seconds. I tested the code in samsung galaxy ace 2.2.1.. what can be the reason for that ?Pape
Is there any flashlight app you can find for that phone that works? If not, I suspect it's something in the hardware that is shutting it off after some point.Laruelarum
I'm the developer of Cool Flashlight and I can confirm that this code works also for galaxy 2, ideios X5 and htc desire.Cyrilcyrill
@mmyer this app works fine in my mobile market.android.com/…..Pape
I am testing this on Samsung Galaxy Ace and it turn ons the flash light and turns it off again after few seconds how can I make it work?Oppress
I
2

In my case for Samsung devices I needed to set focus mode to infinity and it started to work

params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(params);
mCamera.startPreview();
Idonah answered 12/1, 2012 at 23:50 Comment(1)
Thanks a lot for the hint :) I had a problem with flash not working on a Galaxy S 5 with focus mode "auto", setting it to "infinity" did the trick :pBanta

© 2022 - 2024 — McMap. All rights reserved.