[ワーキングコード / 22.01.2013]
次のコードが機能せず、その理由がわかりません。機能するように権限とボタンを設定しました...
次のようにマニフェストにアクセス許可を設定します。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="spicysoftware.spicytaschenlampe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="spicysoftware.spicytaschenlampe.FlashLight"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
そして、それがコードです:(懐中電灯をオンに設定するのに機能しますが、オフにはできません...)
Parameters p;
Camera cam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
check_for_flash();
cam = Camera.open();
cam.startPreview();
p = cam.getParameters();
Button cmd_flash_light = (Button) findViewById(R.id.cmd_flash_light);
cmd_flash_light.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(p.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_TORCH)){
Log.v("FLASH_LIGHT_ON", "TRUE");
//p.setFlashMode(Parameters.FLASH_MODE_OFF);
} else {
Log.v("FLASH_LIGHT_ON", "FALSE");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
cam.setParameters(p);
}
});
}
そして、これは logcat エラーです:
01-22 08:11:00.290: D/AndroidRuntime(12239): Shutting down VM
01-22 08:11:00.290: W/dalvikvm(12239): threadid=1: thread exiting with uncaught exception (group=0x40dfc2a0)
01-22 08:11:00.290: E/AndroidRuntime(12239): FATAL EXCEPTION: main
01-22 08:11:00.290: E/AndroidRuntime(12239): java.lang.RuntimeException: Fail to connect to camera service
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.hardware.Camera.native_setup(Native Method)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.hardware.Camera.<init>(Camera.java:353)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.hardware.Camera.open(Camera.java:330)
01-22 08:11:00.290: E/AndroidRuntime(12239): at spicysoftware.spicytaschenlampe.FlashLight$1.onClick(FlashLight.java:28)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.view.View.performClick(View.java:4223)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.view.View$PerformClick.run(View.java:17275)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.os.Handler.handleCallback(Handler.java:615)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.os.Looper.loop(Looper.java:137)
01-22 08:11:00.290: E/AndroidRuntime(12239): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-22 08:11:00.290: E/AndroidRuntime(12239): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 08:11:00.290: E/AndroidRuntime(12239): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 08:11:00.290: E/AndroidRuntime(12239): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-22 08:11:00.290: E/AndroidRuntime(12239): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-22 08:11:00.290: E/AndroidRuntime(12239): at dalvik.system.NativeStart.main(Native Method)