Pick Video From Gallery in Kotlin Android

In this article, we will discuss how we can pick a video from Gallery in Kotlin and then how we can play that chosen video in the phone’s default video player. You can follow the steps given below to pick all types of videos from the Gallery in Kotlin Android.

Pick Video From Gallery in Kotlin Android

1. Open Gallery to Pick Video

The first step is to open phone’s default gallery App to choose a video to pick in the App. For this purpose you can use the following method.

private fun openGalleryForVideo() {
        val intent = Intent()
        intent.type = "video/*"
        intent.action = Intent.ACTION_PICK
        startActivityForResult(Intent.createChooser(intent, "Select Video"),REQUEST_CODE)
}

2. Receive Chosen Video in onActivityResult Method

The second step is to override the onActivityResult(...) method to receive the chosen video response and its data when the user comes back to the App after picking the video. For this purpose use the following code. Please ignore the missing functions errors, we will add in the next steps.

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

3. Method to Play Picked Video in Phone’s Video Player

Use the following method to play a video in device’s default player, as you can see its used in the above onActivityResult Method.

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

As you can see in the above onActivityResult method, when we get data from Intent, it gives us the URI object. In the above code URIPathHelper class is used to get a full Video path from the URI object. Actually I have already written an article on the topic How to get File from a URI. Just copy that helper class in your code. It works well for all scenarios and for all Android versions.

That’s it. This is how you can Pick Video From Gallery in Kotlin 🙂

Next:

1. Upload Video File to Server in Android Kotlin

2. Capture Video From Camera in Kotlin – Android

3. Pick Image 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: