简体   繁体   中英

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

你必须设置回调mHolder.addCallback(this);

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?

您需要在清单文件中添加此标记。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM