8

我正在开发一个需要打开我的 Galaxy Nexus 设备的手电筒的应用程序。我在这里提到了帖子

Galaxy Nexus 上的 LED 手电筒可由什么 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;
    }




}

显现:

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

但是,我仍然无法打开手电筒。谁能指出我的错误?谢谢

4

3 回答 3

7

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

于 2012-07-13T03:25:38.367 回答
0

尝试添加:

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

看看这篇文章: 如何在 Android 中以编程方式打开相机闪光灯?

于 2012-05-24T09:42:22.840 回答
0

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

<uses-feature android:name="android.hardware.camera.flash"/>
于 2012-05-24T09:42:38.463 回答