我正在尝试使用本教程使用前置摄像头
http://www.vogella.com/articles/AndroidCamera/article.html
但有一个问题让我很生气。
首先,如果使用示例提供的代码,在 onClick(View view) 处,相机为空。这是我的问题吗?但是,我解决了直接在我的 onClick 方法中实现 camera.open() 的问题。
public void takePhoto(View view) {
if (!getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
try{
PhotoHandler photoHandler = new PhotoHandler(getApplicationContext());
camera.takePicture(null, null,photoHandler);
}catch(Exception c){
Log.e("error", c.toString(),c.getCause());
}
}
我加了
try{
PhotoHandler photoHandler = new PhotoHandler(getApplicationContext());
camera.takePicture(null, null,photoHandler);
}catch(Exception c){
Log.e("error", c.toString(),c.getCause());
}
看看有什么问题,logcat 告诉我
05-16 17:19:00.456: E/AndroidRuntime(12638): FATAL EXCEPTION: main
05-16 17:19:00.456: E/AndroidRuntime(12638): java.lang.IllegalStateException: Could not execute method of the activity
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.view.View$1.onClick(View.java:3599)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.view.View.performClick(View.java:4204)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.view.View$PerformClick.run(View.java:17354)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.os.Handler.handleCallback(Handler.java:725)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.os.Looper.loop(Looper.java:137)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.app.ActivityThread.main(ActivityThread.java:5231)
05-16 17:19:00.456: E/AndroidRuntime(12638): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:19:00.456: E/AndroidRuntime(12638): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 17:19:00.456: E/AndroidRuntime(12638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
05-16 17:19:00.456: E/AndroidRuntime(12638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
05-16 17:19:00.456: E/AndroidRuntime(12638): at dalvik.system.NativeStart.main(Native Method)
05-16 17:19:00.456: E/AndroidRuntime(12638): Caused by: java.lang.reflect.InvocationTargetException
05-16 17:19:00.456: E/AndroidRuntime(12638): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:19:00.456: E/AndroidRuntime(12638): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.view.View$1.onClick(View.java:3594)
05-16 17:19:00.456: E/AndroidRuntime(12638): ... 11 more
05-16 17:19:00.456: E/AndroidRuntime(12638): Caused by: java.lang.RuntimeException: **Fail to connect to camera service**
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.hardware.Camera.native_setup(Native Method)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.hardware.Camera.<init>(Camera.java:340)
05-16 17:19:00.456: E/AndroidRuntime(12638): at android.hardware.Camera.open(Camera.java:302)
05-16 17:19:00.456: E/AndroidRuntime(12638): at com.example.cameratest.MainActivity.takePhoto(MainActivity.java:34)
05-16 17:19:00.456: E/AndroidRuntime(12638): ... 14 more
我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameratest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.cameratest.MainActivity"
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>
</manifest>