在 MediaStore.INTENT_ACTION_VIDEO_CAMERA 的情况下没有 startActivityForResult 的回调

No callback for startActivityForResult in case of MediaStore.INTENT_ACTION_VIDEO_CAMERA

案例是当用户选择相机时,他可以灵活地捕捉图像或录制视频,并且用户应该能够在自己的应用程序中显示相同的内容。

对于这种情况,使用了 MediaStore.INTENT_ACTION_VIDEO_CAMERA 意图,但在捕获图像或录制视频的情况下都没有回调。

Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);

获取结果,代码如下:-

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            return;
        }

        if (resultCode == RESULT_OK) {
            if (requestCode == VIDEO_CAMERA) {
                   Uri uri = data.getData();
            }
        }
    }

要启动相机并将结果返回 onActivityResult(),您应该使用 MediaStore.ACTION_IMAGE_CAPTURE 创建一个 Intent:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

同样,要拍摄视频,请使用 MediaStore.ACTION_VIDEO_CAPTURE

终于找到了解决办法,如下:-

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);

终于在onActivityResult收到回调,我是这样得到uri的:-

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            return;
        }

        if (resultCode == RESULT_OK) {
            if (requestCode == VIDEO_CAMERA) {
                    Uri uri;
                    if (data == null || data.getData() == null) {
                        Bitmap bitmap (Bitmap)data.getExtras().get("data");
                        // TODO:Get uri from bitmap for image
                            uri = getImageUri(context, bitmap);
                    } else {
                        //Get uri for video
                        uri = data.getData();
                    }   


            }
       }
}