Java startActivityForResult返回而不调用ActivityResult

Java startActivityForResult返回而不调用ActivityResult,java,android,android-intent,android-camera,android-camera-intent,Java,Android,Android Intent,Android Camera,Android Camera Intent,我正在尝试捕获一张照片并将其保存在SD卡中。生成图像uri,当我调用startActivityForResult时,它将返回而不调用onActivityResult private Uri dispatchTakePictureIntent() { Uri uri = null; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that th

我正在尝试捕获一张照片并将其保存在SD卡中。生成图像uri,当我调用startActivityForResult时,它将返回而不调用onActivityResult

  private Uri dispatchTakePictureIntent() {
      Uri uri = null;
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      // Ensure that there's a camera activity to handle the intent
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
          // Create the File where the photo should go
          File photoFile = null;
          try {
              photoFile = createImageFile();
              Log.d("dispatchTakePictureIntent", "inside try");
          } catch (IOException ex) {
              // Error occurred while creating the File
              Log.d("dispatchTakePictureIntent", "inside catch");

          }
          // Continue only if the File was successfully created
          if (photoFile != null) {

              Log.d("dispatchTakePictureIntent", "inside secondif");
              takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                      Uri.fromFile(photoFile));
              Log.d("dispatchTakePictureIntent","after putExtra");

              startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
              uri = Uri.fromFile(photoFile);
              Log.d("dispatchTakePictureIntent", uri.toString());
              return uri;
          }

      }
      return uri;
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.d("onActivityResult", "outside if");
      ImageView recipeImageView = (ImageView)this.findViewById(R.id.dishImage);
      Log.d("onActivityResult", "outside if");
      Log.d("requestcode", requestCode+"");
      Log.d("REQUEST_IMAGE_CAPTURE", REQUEST_IMAGE_CAPTURE+"");
      Log.d("resultcode", resultCode+"");
      Log.d("REESULT_OK", RESULT_OK+"");
      Log.d("onActivityResult", "inside if");
      if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
          Log.d("onActivityResult", "inside if");
          Bundle extras = data.getExtras();
          Bitmap imageBitmap = (Bitmap) extras.get("data");
          recipeImageView.setImageBitmap(imageBitmap);
      }
      Log.d("onActivityResult", "completed if");
  }


String mCurrentPhotoPath;

  private File createImageFile() throws IOException {
      // Create an image file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      String imageFileName = "JPEG_" + timeStamp + "_";
      File storageDir = Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES);
      Log.d("createImageFile", Environment.DIRECTORY_PICTURES);
      Log.d("createImageFile", storageDir.toString());
      Log.d("createImageFile", imageFileName);
      File image = File.createTempFile(
          imageFileName,  /* prefix */
          ".jpg",         /* suffix */
          this.getCacheDir()     /* directory */
          //storageDir
      );
      Log.d("createImageFile", image.toString());
      // Save a file: path for use with ACTION_VIEW intents
      mCurrentPhotoPath = "file:" + image.getAbsolutePath();
      Log.d("createImageFile", mCurrentPhotoPath);
      return image;
  }
然而,当我只调用缩略图图像而不将其保存在SD卡中时,会调用onActivityResult并显示缩略图图像

 private void dispatchTakePictureIntentThumb() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
当我检查日志消息时

12-23 23:16:15.589:D/createImageFile(2189): /data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

12-23 23:16:15.599:D/createImageFile(2189): 文件:/data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

12-23 23:16:15.599:D/DispatchTakePictureContent(2189):内部尝试

12-23 23:16:15.599:D/DispatchTakePictureContent(2189):内部秒如果

12-23 23:16:15.599:D/分派TakePictureContent(2189):在putExtra之后

12-23 23:16:15.609:E/SoundPool(374):加载/system/media/audio/ui/KeypressInvalid.ogg时出错

12-23 23:16:15.619:W/AudioService(374):声音池无法加载文件:/system/media/audio/ui/keypress invalid.ogg

12-23 23:16:15.619:W/AudioService(374):onLoadSoundEffects(),加载样本时出现错误-1

12-23 23:16:15.629:I/ActivityManager(374):从pid 2189启动u0{act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.camera(有额外功能)}

12-23 23:16:15.689:D/gralloc(51):在创建缓冲区的进程中注册缓冲区。这可能会导致内存排序问题

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23 23:16:15.689:E/SurfaceFlinger(51):glCheckFramebufferStatusOES错误1613735025

12-23 23:16:15.689:E/SurfaceFlinger(51):拍摄屏幕截图时出现GL\u FRAMEBUFFER\u COMPLETE\u OES错误

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23:16:15.689:E/libEGL(51):称为未实现的OpenGL ES API

12-23 23:16:15.689:W/WindowManager(374):屏幕截图故障截图(328x546)至层21040

12-23 23:16:15.909:D/调度画面内容(2189):file:///data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

通过查看其他帖子,我确定以下内容是好的

  • 已启用写入权限
    
    
  • 我调用了startActivityForResult()而不是startActivity()
  • android:launchMode=“singleInstance”,android:noHistory=“true”不在清单中

  • 在功能方面,我们可以打开相机并拍照,但当我们尝试保存时,它仍处于相同的状态

    我遇到了这个问题。
    您是否在活动的属性中使用了android:launchMode=“singleInstance”?

    以下是将图像保存到外部目录的功能

    public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
        String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
        String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
        try {
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }
    

    一旦检查了这一点,你就会明白了……不,我在舱单上没有。是否应添加或删除android:launchMode=“singleInstance”?是否应删除以使onActivityResult正常工作
    public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
        String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
        String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
        try {
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }