如果你注释掉//mCamera.startPreview();
怎么办?你的手电筒能继续工作吗?这是我为使手电筒继续在我的设备上工作而实现的代码。您可以验证您的手电筒是否可以在您的设备上正常工作。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
public class MainActivity extends Activity {
private Button mSwitchButton = null;
private Camera mCamera;
private Camera.Parameters mParameters;
private boolean mbTorchEnabled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSwitchButton = new Button(this);
mSwitchButton.setText("switch");
mSwitchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mParameters = mCamera.getParameters();
List flashModes = mParameters.getSupportedFlashModes ();
if(flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
if(mbTorchEnabled){
//Set the flash parameter to off
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
else{
//Set the flash parameter to use the torch
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
//Commit the camera parameters
mCamera.setParameters(mParameters);
mbTorchEnabled = !mbTorchEnabled;
}
}
});
setContentView(mSwitchButton);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mCamera = Camera.open();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mCamera.release();
}
}