0
我想在android中打開Nexus5閃光燈,但我在logcat中發現錯誤。 可以打開它嗎?Nexus5閃光燈問題?
我的代碼是在這裏:
活動的Java
public class MainActivity extends Activity {
Camera camera;
Parameters parameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (switch1.isChecked()) {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
Toast.makeText(getApplicationContext(),
"Turning on FlashLight..", Toast.LENGTH_LONG)
.show();
} else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
Toast.makeText(getApplicationContext(),
"Turning off FlashLight..", Toast.LENGTH_LONG)
.show();
}
}
});
}
}
,我使用這些權限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera.autofocus" />
05-07 10:08:26.970: W/CameraBase(19606): An error occurred while connecting to camera: 0
05-07 10:08:26.970: D/AndroidRuntime(19606): Shutting down VM
05-07 10:08:26.970: W/dalvikvm(19606): threadid=1: thread exiting with uncaught exception (group=0x41682ba8)
05-07 10:08:26.970: E/AndroidRuntime(19606): FATAL EXCEPTION: main
05-07 10:08:26.970: E/AndroidRuntime(19606): Process: com.systelligence.androidflashlight, PID: 19606
05-07 10:08:26.970: E/AndroidRuntime(19606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.systelligence.androidflashlight/com.systelligence.androidflashlight.MainActivity}: java.lang.RuntimeException: Fail to connect to camera service
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.access$900(ActivityThread.java:135)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.os.Looper.loop(Looper.java:136)
這是什麼錯誤? – chrylis
顯示你的logcat。 – prakash
相關:[閃光](http://stackoverflow.com/questions/6068803/how-turn-on-camera-flash-light-programmatically-in-android) – prakash