1

我正在使用本教程:

http://www.vogella.com/articles/AndroidCamera/article.html

捕获图像并将其保存在 SD 卡上,但是每当我运行此程序时,都会得到:不幸的是应用程序已停止

错误说:

引起:java.lang.reflect.InvocationTargetException

第 39 行是:

camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));

日志输出:

05-29 16:19:38.351: E/AndroidRuntime(767): FATAL EXCEPTION: main
05-29 16:19:38.351: E/AndroidRuntime(767): java.lang.IllegalStateException: Could not execute method of the activity
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3599)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:4204)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$PerformClick.run(View.java:17355)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.handleCallback(Handler.java:725)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 16:19:38.351: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.reflect.InvocationTargetException
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3594)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 11 more
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

MakePhotoActivity.java:

  public class MakePhotoActivity extends Activity {
  final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private int cameraId = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    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);
      }
    }
  }

  public void onClick(View view) {
    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));
  }

  private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

  @Override
  protected void onPause() {
    if (camera != null) {
      camera.release();
      camera = null;
    }
    super.onPause();
  }
4

5 回答 5

5

要打开 Android 默认相机,您需要编写简单的 2 行代码,它们是:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

并获取您需要 Override 的单击图像的路径onActivityResult()。下面的代码帮助你获取点击图片的路径:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...
  }
}

最后,在您的清单文件中,为相机添加一个“Uses-Permission”。

<uses-permission android:name="android.permission.CAMERA" />

:更新答案:

对于定制相机

按照此链接获取自定义相机:

  1. 自定义相机应用程序
  2. 为测试、玩耍和学习而创建的 Android 定制相机
  3. 如何在 Android 中使用自定义相机捕获和保存图像?
  4. 如何在 Android 中为您的相机创建自定义布局?
于 2013-05-29T11:52:11.390 回答
2

您通过使用反射调用方法添加了额外的抽象级别。反射层将任何异常包装在一个 中InvocationTargetException,这使您可以区分实际上是由反射调用中的失败(例如,您的参数列表无效)引起的异常和调用的方法中的失败之间的区别。

只需解开其中的原因InvocationTargetException,您就会得到原来的原因。

于 2013-05-29T11:29:52.110 回答
0

似乎您的camera对象为空

于 2013-05-29T11:21:25.313 回答
0

我认为你的问题在这里:

Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

检查哪里出了问题@MakePhotoActivity.java,第 39 行。

于 2013-05-29T11:04:39.850 回答
-3

您应该需要 AVD 上的摄像头所以编辑您的 AVD 并在背面和前面添加摄像头.....

于 2013-08-05T05:08:19.677 回答