15

I'm facing to a problem with a Samsung Galaxy Tab. I want to use the camera flash as torch.

Does anyone know how to enable it ?

Hereby a code that works to enable/disable the camera flash on a HTC Desire but fails on Samsung Galaxy Tab.

FlashLight.java :

package com.example.FlashLight;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FlashLight extends Activity {
    private final static String LOG_TAG = "FlashLight";

    private Button mOnBtn;
    private Button mOffBtn;

    private Camera mCamera;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mOnBtn = (Button) findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                processOnClick();
            }
        });

        mOffBtn = (Button) findViewById(R.id.off_btn);
        mOffBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                processOffClick();
            }
        });
   }

    @Override
    protected void onResume() {
        super.onResume();
        try{
            mCamera = Camera.open();
        } catch( Exception e ){
            Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
        }
    }

    @Override
    protected void onPause() {
        if( mCamera != null ){
            mCamera.release();
            mCamera = null;
        }
        super.onPause();
    }

    private void processOffClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_OFF );
            mCamera.setParameters( params );
        }
    }

    private void processOnClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_TORCH );
            mCamera.setParameters( params );
        }
    }
}

AndroidManifest.xml :

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FlashLight"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

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

layout/main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button  
        android:id="@+id/on_btn"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Flash ON" />

    <Button  
        android:id="@+id/off_btn"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Flash OFF" />
</LinearLayout>

Thanks !

Sly
  • 2,065
  • 5
  • 21
  • 28
  • 1
    Also, i would remove "flash" as a tag as its confusing it with the runtime environment Flash. – Will Tate Feb 16 '11 at 14:20
  • Thanks willytate, I replace "flash" by "led" – Sly Feb 16 '11 at 14:57
  • @TeddyBearFr i have try your code in my HTC WildFire A3333 it is working fine with Flash Light on And Off ,But Same Code Will not Work in Android Samsung Galaxy Ace s-5830 mobile any idea to do right to work in both devices. – Herry Aug 24 '11 at 09:46

4 Answers4

22

You aren't doing anything wrong. In fact, you are doing everything correct. You are encountering a Device-Specific issue that is very prevalent in the Android world. I have found the following behavioral patterns for FLASH_MODE_TORCH:

  • Works fine in all cases
  • Works fine, but not with autofocus on
  • Doesn't work at all

Frustratingly, getSupportedFlashModes() will return FLASH_MODE_TORCH on nearly every device when only a handful actually support it.

Also, some device implementations swizzle the supported flash modes. If you go through Camera.Parameters you can try setting the flash mode to FLASH_MODE_ON, FLASH_MODE_AUTO or FLASH_MODE_RED_EYE and see whether any of them work. Note - this is a device-specific hack.

I have filed these types of bugs with Google regarding the DroidX and Nexus S. They closed it as a device-specific issue. I would say to report this to Samsung in hopes for a driver or firmware fix, but their Android support channels do not exist.

Community
  • 1
  • 1
Error 454
  • 7,063
  • 2
  • 30
  • 46
  • Tkanks Error 454 for your answer. getSupportedFlashModes() returns "on", "off", "auto" and "torch" values on Galaxy Tab but none of them works to enable the camera LED. I download a free application from the Android market called "Tiny Flashlight + LED" and released by Nikolay Ananiev. It works with a Galaxy Tab ! However, I don't know how he did... – Sly Feb 16 '11 at 15:34
  • Sometimes, setting the parameter directly will work as a hack. So instead of *params.setFlashMode( Parameters.FLASH_MODE_TORCH )* you can try *params.set("flash-mode", "torch")* try replacing "torch" with "on" or "auto" as well. – Error 454 Feb 16 '11 at 19:32
  • 1
    TeddyBearFr - did you find out if the params.set() suggestion works ? I have a camera app where a user has reported this exact issue on a Galaxy Tab, but I do not such a device, so I can not test it. Thanks. – hrstrand Feb 19 '11 at 21:18
7

Took me a while but I think you're missing a startPreview() there.

After you do your Camera.open() and after you set the parametrs, do a mCamera.startPreview(). That should do the trick.

DarthJDG
  • 16,140
  • 11
  • 47
  • 55
JM Macariola
  • 103
  • 1
  • 4
2

This is how I made it work.

if (Build.MODEL.equals("GT-P1000")) {

            Log.d(FlashlightActivity.TAG, "This is Samsung Galaxy Tab.");

            params.setFlashMode(Parameters.FLASH_MODE_ON);
            camera.setParameters(params);
            camera.startPreview();
            camera.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

            isLEDturnedOn = true;
            Log.d(FlashlightActivity.TAG, "LED turned ON.");

} 
urSus
  • 11,791
  • 12
  • 60
  • 86
2

This is how I turn on and of the torch in LG Nexus 4 and Samsung Galaxy Ace 2.

public void changeTorch() {
            try {
                camera = Camera.open();
                // try to open the camera to turn on the torch
                Camera.Parameters param = camera.getParameters();
                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(param);
                camera.startPreview(); // needed for some devices
                Log.v("BSW torch", "Torch ON");
            } catch (Exception e) {
                // if open camera fails, try to release camera
                Log.w("BSW torch", "Camera is being used trying to turn Torch OFF");
                try {
                    camera.release();
                } catch (Exception ex) {
                    Log.e("BSF torch", "Error releasing camera");
                }
            }
        }
Freefri
  • 658
  • 7
  • 21