【问题标题】:Camera Intent Not Adding Extra相机意图未添加额外
【发布时间】:2017-01-25 01:02:39
【问题描述】:

我正在使用隐式 Intent 拍照。我一直在关注tutorial 中概述的工作。我遇到的问题是未交付添加到 Intent 的额外内容。这是我正在使用的代码:

private void dispatchTakePictureIntent(Context context) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile(this.getActivity());
            } catch (IOException ex) {
                Log.e(TAG, "Error creating file: " + ex.toString());
                //TODO: 2017/1/24 - Handle file not created
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Error")
                        .setMessage(ex.toString());

                final AlertDialog dialog = builder.create();
                dialog.show();

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(context,
                        "com.example.myapp",
                        photoFile);
                //THIS EXTRA IS NOT BEING ADDED TO THE INTENT
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

                galleryAddPic(context, photoFile.getAbsolutePath());
            }
        }
    }

onActivityResult 方法被触发时,Intent 为空。这是代码:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //These extras are empty. I have used the debug tool, and there is nothing in here. 
        Bundle extras = data.getExtras();
        //extras is null
        imageBitmap = (Bitmap) extras.get("data");
        previewImage.setImageBitmap(imageBitmap);
    }
}

为什么意图是空的?我需要做什么来解决这个问题?

【问题讨论】:

    标签: android android-intent android-camera


    【解决方案1】:

    为什么意图是空的?

    因为您要求它为空,因此在您的 ACTION_IMAGE_CAPTURE 请求中包含 EXTRA_OUTPUT。引用the documentation:

    调用者可能会传递一个额外的 EXTRA_OUTPUT 来控制此图像的写入位置。如果 EXTRA_OUTPUT 不存在,则将小尺寸图像作为额外字段中的位图对象返回。这对于只需要小图像的应用程序很有用。如果存在 EXTRA_OUTPUT,则将全尺寸图像写入 EXTRA_OUTPUT 的 Uri 值。

     

    我需要做什么来解决这个问题?

    要么:

    • 去掉EXTRA_OUTPUT(如果你想要缩略图大小的图像),或者

    • 停止寻找额外的"data",并查看您在EXTRA_OUTPUT中指定的位置

    【讨论】:

    • 这很有意义。当我第一次这样做时,我使用的是缩略图。谢谢你的回答!
    • @BlackHatSamurai:进一步说明:当相机活动处于前台时,您的进程可能会终止。确保在保存的实例状态Bundle 中保持photoFile,以便在发生这种情况时拥有它,并且作为最终触发onActivityResult() 的一部分重新创建您的活动。
    • 谢谢。我一定会这样做的。这是非常有用的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多