0

我正在尝试使用本教程使用前置摄像头

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>
4

2 回答 2

2

好的,我解决了这个问题。教程错了。Android教程告诉你

Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.

教程没有这样做。现在一切正常。

于 2013-05-21T09:04:36.593 回答
0

相机可能需要一些时间才能打开。您可以在您的活动中启动相机onResume(),这将保证当用户单击按钮时,相机被初始化。

当您不再需要它时,不要忘记优雅地释放相机,例如在onPause().

PS:在教程中,相机在onCreate(). 这不是最好的选择,特别是如果应用程序可能会暂停并稍后重新启动,因为之后onPause()相机将为空并且onCreate()如果系统决定让您的活动保持活动状态可能不会被调用。

于 2013-05-16T21:08:59.030 回答