No callback for startActivityForResult in case of MediaStore.INTENT_ACTION_VIDEO_CAMERA

728 Views Asked by At

The case is when user selects camera, he has the flexibility to either capture image or record video and the user shall be able to show the same in one's app.

For this case, MediaStore.INTENT_ACTION_VIDEO_CAMERA intent is used but no callback comes in either case of capturing image or recording video.

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

For getting result, code used as follows:-

@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();
            }
        }
    }
2

There are 2 best solutions below

0
On BEST ANSWER

I finally found a solution and it is as follows:-

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);

And finally receives the callback in onActivityResult and I got the uri in this way:-

@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();
                    }   


            }
       }
}
4
On

To start the camera and get the result back in onActivityResult(), you should create an Intent with MediaStore.ACTION_IMAGE_CAPTURE:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Similarly, to capture a video, use MediaStore.ACTION_VIDEO_CAPTURE