Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Flashlight of Galaxy Nexus

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

like image 1000
user1311794 Avatar asked May 24 '12 09:05

user1311794


People also ask

How do I turn the flashlight on my Samsung phone?

To use the flashlight, open the Quick settings panel by swiping down from the top of the screen using two fingers. Next, tap the Flashlight icon to turn the light on or off.

How do I use the flashlight on my Samsung home screen?

With Samsung's Android 10 update, you can add the flashlight as a lock screen shortcut. Open the Settings app and go to "Lock screen," then "Shortcuts." Choose which side you want the shortcut on by hitting either "Right shortcut" or "Left shortcut," then pick "Flashlight" towards the top of the next screen.

How do I turn on my flashlight with the volume button?

Turn on the Flashlight with the Volume Buttons You can even enable the Flashlight from your lock screen, but you will need to go into the app's settings to allow that. To access Settings, just open the app and tap on the three vertical dots on the top right of your display.


1 Answers

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

like image 62
Kingkan Banyenngam Avatar answered Sep 28 '22 13:09

Kingkan Banyenngam