java - 拍照并点击确定后相机崩溃

标签 java android android-camera

我正在开发一个发送拍照 Intent 的应用程序。我使用的是 Galaxy S5,但此问题已在 3 种不同的设备上得到确认。

首先,相机开始循环,点击确定后返回拍摄另一张照片。我重构了代码并解决了那个问题,但还有其他问题。现在,在调出拍照屏幕后,使用后退按钮成功返回并在拍照后点击重试工作正常。然而,点击 OK 会做以下三件事之一:

  • 作品(~%20)
  • 在我自己的错误处理程序之外 抛出一个错误。 “很遗憾,MyApp 意外停止”。然后它返回到调用 Activity ,从我的处理程序中抛出一个错误,因为现在它正在尝试从现在为 null 的内容中读取(大概是因为应用程序崩溃了)。断点现在被忽略,我必须再次启动调试。我可以返回到应用程序的第一页并再次执行该过程,但调试没有响应。 (~30%)
  • 除了抛出我的错误外,与之前相同,我点击后退按钮摆脱它,然后点击后退按钮返回屏幕,然后我得到“不幸的是我的应用程序已停止”。 (~50%)

没有能够找到从相机抛出的错误,它会导致最初的怪异,然后导致一切崩溃。我在 onActivityResult 上有一个断点,如果抛出错误,它永远不会被触发。此外,我在 startActivityForResult 上的 catch block 从不抛出该错误。错误总是来自 onCreate 中的 NPE,因为在拍摄照片后 Activity 试图再次启动或应用程序抛出“不幸的是我的应用程序已停止”。

我有很多详细信息,但我不想让信息过多。基本上,当我点击 OK 时,它要么在大约 20% 的时间内工作,要么在其他 80% 的时间内完全崩溃。谁能指出我正确的方向,为什么结果看起来如此随机和不可预测?谢谢!

我一直在关注这个教程: http://developer.android.com/training/camera/photobasics.html

调用 Activity

 private class SetCounterActivityMenu implements OnMenuItemClickListener {
  private FieldAppActivity a;
  private SetCounterActivityMenu(FieldAppActivity _a) {
    this.a = _a;
  }

  @Override
  public boolean onMenuItemClick(MenuItem view) {
    switch (view.getItemId()) {
    case 777771: {
      try{
        //nextActivity(CameraActivityNative.class);
        cameraAN = new CameraActivityNative(handler,SetCounterActivity.this);
        Intent intent = cameraAN.getPictureIntent();
        startActivityForResult(intent, cameraAN.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
      }
      catch (Exception ex){
        handler.error("Error occured taking picture", ex);
      }
      break;
    }
    case 777772: {
      a.showMap(MapShowsWhat.survey);
      break;
    }
    }
    return false;
  }
}

CameraActivityNative.java

public Intent getPictureIntent() {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // create a file to save the image
    try {
      fileUri = createMultimediaFile(MEDIA_TYPE_IMAGE, handler);
    }
    catch (Exception e){
      Utils.log("Camera Activity Native", "cannot create file", 'e');
    }
    // set the image file name
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image Intent
    return intent;
  }
private Uri createMultimediaFile(int type,    FieldApplicationManager.FAMEventHandler handler) throws   UnsupportedOperationException, IOException{
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

  File mediaStorageDir = new   File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyDir");

  // Create the storage directory if it does not exist
  if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
      Utils.log("CameraActivityNative", "failed to create directory", 'd');
      return null;
    }
  }

  // Create a media file name
  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  String imageFileName;

  String fileSuffix;
  if (type == MEDIA_TYPE_IMAGE){
    imageFileName = "IMG_"+ timeStamp + ".jpg";
  }
  else if (type == MEDIA_TYPE_VIDEO) {
    imageFileName = "VID_"+ timeStamp + ".mp4";
  }
  else {
    throw new UnsupportedOperationException("Media type not supported");
  }

  File mediaFile = new File(mediaStorageDir, imageFileName);

  // Save a file: path for use with ACTION_VIEW intents
  // galleryAddPic("file:" + mediaFile.getAbsolutePath());
  return Uri.fromFile(mediaFile);
}

日志错误。我不确定这是否真的相关,因为这是相机已经崩溃后引发的错误。

02-27 17:56:27.609  17153-17153/android.fieldteam E/ERROR﹕ Message: Attempt to read from field 'java.lang.String android.api.CountSurvey.stationid' on a null object reference
    Last System Info: null
    Parameter: onGCFCreate
    Stack Trace: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.api.CountSurvey.stationid' on a null object reference
            at android.gui.SetCounterActivity.onGCFCreate(SetCounterActivity.java:33)
            at android.gui.FieldAppActivity.onCreate(FieldAppActivity.java:64)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

最佳答案

我很抱歉没有更清楚地问这个问题,因为我自己也不确定。结果一次出现了多个问题,我错误地认为它们都是同一个问题。希望这会在将来帮助其他人。这是我遇到的问题的总结

  1. 每当它从相机 Intent 返回时,调试器就是
    分离(我认为这是因为相机的错误
    但结果有点正常)

  2. 当从相机 Intent 返回时,应用程序有时会被操作系统破坏(我假设内存不足
    原因)。这导致了零星的行为和我的状态对象
    返回时会有几个空字段,使我的应用程序崩溃。

    • 修复:在发送图片之前将状态对象写入文件,加载我处理 onCreate 的父 Activity 类,然后转到 子类 Activity 。
  3. 当应用被销毁时,先调用 onCreate 然后再调用 onActivityResult,而不是在应用未被销毁时才调用 onActivityResult。当我在创建时调用相机 Intent 时,这导致了奇怪的行为。

    • 修复:检查 savedInstanceState 是否为空,以检查 Activity 是否有以前的数据,如果没有,它会启动相机。这解决了有时会陷入拍照和返回相机的无限循环的问题。
  4. 由于其他问题,错误并不总是出现在我的正常错误 toast 或我的日志中(有时与调试器分离),这使得调试变得更加困难,因为它不一致。

    • 与#1 相同。

关于java - 拍照并点击确定后相机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776445/

相关文章:

Android:BroadcastReceiver Intent 检测拍摄的相机照片?

java - Eclipse 使用源代码编译 .jar 文件,并且没有 .class 文件

java - 用不允许出现的次数替换字符串中的所有连续重复项

android - 如何检查照片是在 Android 中以横向还是纵向模式拍摄的?

android - 包 android.support.v4.view 不存在,即使在为它添加依赖项之后

android - Android Gradle SourceSet资源不复制基本目录

java - Textureview.getBitmap() 使主 Ui 线程变得缓慢

Java InterruptedException - 我对线程被中断意味着什么或为什么会被中断感到困惑

java - Akka 是否有一个 ExecutorCompletionService 等效项,其中 Futures 按其完成时间排队?

java - Android UnknownHostException : is there a way to set timeout?