我正在尝试使用本教程的前置摄像头
http://www.vogella.com/articles/AndroidCamera/article.html
但是遇到了一个令我生气的问题。
首先,如果使用示例提供的代码,则在onClick(视图视图)中,摄像机为空。这是一个地雷问题吗?但是,我解决了直接在我的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>
答案 0 :(得分:2)
好的,我解决了这个问题。 教程错了。 Android教程告诉
Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
并且教程没有这样做。 现在一切正常。
答案 1 :(得分:0)
相机可能需要一些时间才能打开。您可以在活动的onResume()
中启动相机,这可以保证当用户点击按钮时,相机会被初始化。
当您不再需要时,不要忘记优雅地释放相机,例如在onPause()
。
PS:在教程中,相机在onCreate()
中初始化。这不是最好的选择,特别是如果应用程序可能暂停并稍后重新启动,因为在onPause()
之后,如果系统决定让您的活动保持活动状态,那么相机将为空并且onCreate()
可能不会被调用。 / p>