Capture Video From Camera in Kotlin Android

In this article, we will discuss how to open the Default camera App in our App to capture video from the camera in Kotlin Android. After capturing video from the camera we will also play that video in the device’s default video player to check is the video recorded and saved perfectly. Following is the Kotlin code written step by step to capture video from the camera through Intent.

Record Video in Kotlin Android

1. Open Camera App to Capture Video

First, we check if the camera is available because there are many Android devices that don’t have a camera to App can be crashed. Then we initialize Intent with ACTION_VIDEO_CAPTURE action to open the default camera app to record a video.

private fun openCameraToCaptureVideo() {
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) { // First check if camera is available in the device
            val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
            startActivityForResult(intent, REQUEST_CODE);
        }
    }

2. Handle Recorded Video in onActivityResult Method

Now override the onActivityResult(...) method to receive the captured video when use comes back to our Activity. We receive data in Intent and from that data we get Uri of the saved video file. For this purpose use the following code. Please ignore the missing functions errors, we will discuss in the next steps.

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        super.onActivityResult(requestCode, resultCode, intent)
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
            if (intent?.data != null) {
                val uriPathHelper = URIPathHelper()
                val videoFullPath = uriPathHelper.getPath(this, intent.data) // Use this video path according to your logic
                // if you want to play video just after recording it to check is it working (optional)
                if (videoFullPath != null) {
                    playVideoInDevicePlayer(videoFullPath);
                }
            }
        }
    }

3. Method to Play Recorded Video in Device’s Default Video Player (Optional)

As you can see in the above onActivityResult(...) code that we have called the method playVideoInDevicePlayer(videoFullPath) to play the recorded video. Following is the definition of that function.

fun playVideoInDevicePlayer(videoPath: String) {
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(videoPath))
            intent.setDataAndType(Uri.parse(videoPath), "video/mp4")
            startActivity(intent)
    }

4. Get Video File Path from URI

In our onActivityResult method, we received an Intent object from which we are getting Uri object by intent?.data. So most of the time we need to get the full video file path from that received Uri object. Depending on the different Android versions the method is different to get the Full path from Uri. I have already shared a Utility class written in Kotin to get a full file path from a Uri object.
Utility class to get file path from a URI
Just copy that URIPathHelper class and paste it in your project.

That’s it. This is how you can Capture Video in Kotlin Android 🙂

Next:

1. Upload Video to Server in Android Kotlin

2. Pick Video From Gallery in Kotlin Android

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: