Android Intent for Capturing both Images and Video

2020-02-24 12:39发布

问题:

Is there an Intent for starting a camera with options to capture both Pictures and Videos on Android?

I've used both MediaStore.ACTION_VIDEO_CAPTURE and MediaStore.ACTION_IMAGE_CAPTURE to capture either audio or video, but I can't find an Intent that will get the option for switching between both of them, as in this example app:

Thanks!

回答1:

It is not possible to capture both image and video using the same intent, Your options are

1) Create your own camera this repo can be a good start But it is going to be a too much effort.

2) Use the Chooser Intent and pass the intent for both image and video, this will give you the option to choose between application which record video and camera separately. In this you cannot do both the things at same time but can choose application according to what you want to do, capture an image or record a video. Below is the code that works for me.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
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, CAPTURE_MEDIA_RESULT_CODE);


回答2:

I could capture both image and video by using the below code.

Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);


回答3:

I achieved it :) You can do it by following --

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
            contentSelectionIntent.setType("*/*");
    intentArray = new Intent[]{takePictureIntent,takeVideoIntent};
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
    startActivityForResult(chooserIntent, 1);

Similar example here

Happy coding :)