开启Galaxy Nexus的手电筒

我正在开发一个应用程序,需要打开我的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; } } 

performance:

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

但是,我仍然不能打开手电筒。 任何人都可以指出我的错误? 谢谢

Solutions Collecting From Web of "开启Galaxy Nexus的手电筒"

你必须设置callbackmHolder.addCallback(this);

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

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

尝试添加:

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

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