【问题标题】:Android taking camera return empty intentAndroid 拍摄相机返回空意图
【发布时间】:2016-03-31 04:24:36
【问题描述】:

我正在开发 android 应用程序,我的应用程序有按钮来拍照。之前我陷入了拍照后onActivityResult返回数据为null的状态。这是相机预期的行为,如果我们将 EXTRA_OUTPUT 放入 intent 中,它将返回 null。出于这个原因,我做了空检查代码,它运行良好。

几天后又一次,我测试了。我仍然再次陷入同样的​​问题。但是这次data 不是nulldata 具有空意图,例如 intentdata.getData() 变为 null。我通过检查 data.getData() == null 解决了这个问题,它再次工作。我不明白为什么会这样。只是好奇发生了什么。出于这个原因,我必须再次重新上传到生产环境。 :-(

//camera intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra("requestCode", Constants.REQUEST_IMAGE_CAPTURE);

Intent chooseImageIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
chooseImageIntent.setType("image/* video/*");
chooseImageIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
chooseImageIntent.putExtra("requestCode", Constants.REQUEST_CHOOSE_FROM);

//app can use camera
if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
    //add output file path which camera will save image to
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Helpers.getOutputMediaFileUri());
    //create choose
    Intent chooser = Intent.createChooser(chooseImageIntent, "Select From");
    //add take camera intent as first intent
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{takePictureIntent});
    //open up dialog
    ((Activity) mContext).startActivityForResult(chooser, Constants.REQUEST_CHOOSE_FROM);
} else {
    ((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}

已编辑

我知道如何解决问题。我不明白的是,如果我输入 EXTRA_OUTPUT,则返回 data 必须是 null。最重要的是我几周前实现的代码,我很确定data 返回null,突然它又不是null 值了。

【问题讨论】:

  • 不要使用data.getData(),而是使用data.getExtras().get("data");,看看问题是否再次出现。
  • 我不使用data.getExtras() 的原因是因为如果我在intent 中传递extra_output,data 变成null。如果我没记错,那就是事实

标签: android android-intent camera


【解决方案1】:
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.

so try this


 void opencameraForPicture(int requestCode, Uri fileUri) {
        checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);


        Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        /* start activity for result pass intent as argument and request code */
        startActivityForResult(intent, requestCode);

    }

    /**
     * This method set the path for the captured image from camera for adding
     * the new  picture in the list
     */
    private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "."
                + Constants.CONTAINER);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + System.currentTimeMillis() + ".png");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }

in @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

String imagePath = fileUri.getPath();

//you can decode this path as bitmap

}

【讨论】:

  • 感谢@Rohit 的回答,但我看不出我的代码有什么不同。你能指出为什么intent返回的数据突然变成了非空值吗?
猜你喜欢
  • 2014-04-28
  • 2011-07-15
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2013-06-29
  • 2011-05-22
  • 1970-01-01
  • 2011-10-10
相关资源
最近更新 更多