7

I am developing an app which needs to open the flashlight of my Galaxy Nexus device. I have referred to the post here

LED flashlight on Galaxy Nexus controllable by what API?

public class TestCamera extends Activity implements SurfaceHolder.Callback{
Camera mCamera;
public static SurfaceView preview;
public static SurfaceHolder mHolder;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    preview = (SurfaceView) findViewById(R.id.camSurface);
    mHolder = preview.getHolder();
    mCamera = Camera.open();
    try {
        mCamera.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Button onLEDbtn = (Button) findViewById(R.id.onLED_btn);
    onLEDbtn.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
            Parameters params = mCamera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(params);      
            mCamera.startPreview();
        }

    });
}



}


    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }


    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mHolder = holder;
        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
         mCamera.stopPreview();
            mHolder = null;
    }




}

Manifest:

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

However, I still cant switch on the flashlight. Could anyone point out my errors? Thanks

Community
  • 1
  • 1
user1311794
  • 199
  • 3
  • 8
  • you can find everything in here http://stackoverflow.com/questions/6068803/how-turn-on-only-camera-flash-light-programmatically-in-android – user219882 May 24 '12 at 09:40
  • possible duplicate of [Unable to use the flashlight in Galaxy Nexus](http://stackoverflow.com/questions/10731285/unable-to-use-the-flashlight-in-galaxy-nexus) – Andro Selva May 24 '12 at 09:47
  • Above is my manifest file Moreover, I added the camSurface view inside main.xml Will it be a problem? I can get the toast message but cannot switch on the flashlight... Please help. Thanks!! – user1311794 May 24 '12 at 10:41
  • 4
    Solved! mHolder.addCallback(this); should be add after mHolder = preview.getHolder(); – user1311794 May 24 '12 at 11:21
  • 1
    you should accept the answer below – Guy Aug 23 '12 at 07:55

3 Answers3

7

you must to set call back mHolder.addCallback(this);

Kingkan Banyenngam
  • 123
  • 1
  • 1
  • 6
0

Try to add :

android:name="android.permission.FLASHLIGHT"
android:name="android.hardware.camera.flash

And look this post : How to turn on camera flash light programmatically in Android?

Community
  • 1
  • 1
Skies
  • 407
  • 2
  • 6
0

You need to add this tag in manifest file.

<uses-feature android:name="android.hardware.camera.flash"/>
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61