Skip to content

3. Picking Videos

Kumar Bibek edited this page Mar 1, 2016 · 4 revisions

Picking videos


To pick video/videos

Use VideoPicker to pick and video or videos.

It is mandatory to set a VideoPickerCallback before triggering the pickVideo() method, or an exception will be raised.

VideoPicker videoPicker = new VideoPicker(Activity.this);
videoPicker.setVideoPickerCallback(new VideoPickerCallback(){
        @Override
        public void onImagesChosen(List<ChosenImage> images) {
            // Display images
        }

        @Override
        public void onError(String message) {
            // Do error handling
        }
    }
);
// videoPicker.allowMultiple(); // Default is false
// videoPicker.shouldGenerateMetadata(false); // Default is true
// videoPicker.shouldGeneratePreviewImages(false); // Default is true
videoPicker.pickImage();

After this call, you need to submit the onActivityResult(int requestCode, int resultCode, Intent data) to VideoPicker so that the processing might start.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_VIDEO_DEVICE) {
            videoPicker.submit(data);
        }
    }
}

The VideoPickerCallback will be triggered once the processing is done.

The one thing you have to handle is when your Activity is killed when the user is still choosing a photo. In such a scenario, VideoPicker reference will be destroyed since the Activity will be re-created. An additional check is required to handle this scenario. You just have to create a new VideoPicker object, attach the callback and call the VideoPicker.submit(data) method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_IMAGE_DEVICE) {
            if(videoPicker == null) {
                 videoPicker = new VideoPicker(Activity.this);
                 videoPicker.setVideoPickerCallback(videoPickerCallback);
            }
            videoPicker.submit(data);
        }
    }
}
Capture a photo using camera

Use CameraVideoPicker to get a photo captured using the device's camera.

It is mandatory to set a VideoPickerCallback before triggering the pickVideo() method, or an exception will be raised. While most of the code is similar to the VideoPicker, there's one important difference between the two.

CameraVideoPicker videoPicker = new CameraVideoPicker(Activity.this);
videoPicker.setVideoPickerCallback(new VideoPickerCallback(){
        @Override
        public void onVideosChosen(List<ChosenVideo> videos) {
            // Display images
        }

        @Override
        public void onError(String message) {
            // Do error handling
        }
    }
);
// videoPicker.shouldGenerateMetadata(false); // Default is true
// videoPicker.shouldGenerateThumbnails(false); // Default is true
String outputPath = videoPicker.pickImage();

Notice, String outputPath = videoPicker.pickVideo(); method. The return value is a String, which is where the captured image from the camera is going to be saved. You can save this string variable during the onSaveInstanceState callback of the Activity.

@Override
    protected void onSaveInstanceState(Bundle outState) {
        // You have to save path in case your activity is killed.
        // In such a scenario, you will need to re-initialize the CameraVideoPicker
        outState.putString("picker_path", outputPath);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // After Activity recreate, you need to re-initialize these
        // two values to be able to re-initialize CameraVideoPicker
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("picker_path")) {
                outputPath = savedInstanceState.getString("picker_path");
            }
        }
        super.onRestoreInstanceState(savedInstanceState);
    }

After this call, you need to submit the onActivityResult(int requestCode, int resultCode, Intent data) to CameraVideoPicker so that the processing might start.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_IMAGE_CAMERA) {
            videoPicker.submit(data);
        }
    }
}

The VideoPickerCallback will be triggered once the processing is done.

The one thing you have to handle is when your Activity is killed when the user is still choosing a video. In such a scenario, CameraImagePicker reference will be destroyed since the Activity will be re-created. An additional check is required to handle this scenario. You just have to create a new CameraVideoPicker object, re-initialize it, attach the callback and call the CameraVideoPicker.submit(data) method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_VIDEO_DEVICE) {
            if(videoPicker == null) {
                 videoPicker = new CameraVideoPicker(Activity.this);
                 videoPicker.reinitialize(outputPath);
                 // OR in one statement
                 // videoPicker = new CameraVideoPicker(Activity.this, outputPath);
                 videoPicker.setVideoPickerCallback(videoPickerCallback);
            }
            videoPicker.submit(data);
        }
    }
}
Clone this wiki locally