Android Pick Video From Gallery Or Camera Programmatically

Android Pick Video from gallery or camera Programmatically tutorial with example is for you here.

The definition of this example is to select or get the video from gallery and show it in the videoview.

This tutorial will also choose or take or capture the video from the camera and play this recorded video in the videoview.

We will also learn how to save the video (which is picked up from the gallery) in the internal storage. After recording or capturing the video from camera, we will save it in the internal storage.

You will get to know how to get video path from gallery in android studio. We will get the video path from uri.

Now follow all the steps to complete this android pick video from gallery or camera tutorial.

After all coding practice you should get the output like below images.

android pick video from gallery or camera

Screen 1

android pick video from gallery or camera

Screen 2

android pick video from gallery or camera

Screen 3

android pick video from gallery or camera

Screen 4


android pick video from gallery or camera

Screen 5

android pick video from gallery or camera

Screen 6


Step 1. Giving Permissions

Give below three permissions in the android manifest file.

We are storing video in the internal storage, so first two permissions are regarding this.

Last one is giving permission to use the camera of the android device.

If your app is targeting the sdk version greater than 22, then you must ask for runtime permissions.

Checkout here how to ask for runtime permissions in android.

I have set the target sdk version as “22“, so I do not need to ask for the runtime permissions in this tutorial.

Step 2. Coding Final Lines

Add below source code into activity_main.xml file

One button and one videoview is present here.

Button is used to decide whether to pick video from gallery or camera and videoview will play the selected the video for the reference.

Copy and paste following source code into file

Let us learn all the above code blocks step by step.

First of all, required variables are declared.

Videoview and button is there in first two lines.

In the third line, folder name is there, in which, selected video will be saved.

Two constants are there in the fourth line.

Pay attention at below method

Above method will be called when the user clicks on the button.

After the button click, one dialog with two options will be opened. First option is to choose video from gallery and second is to capture video from the camera.

When the user select first option chooseVideoFromGallery() method is invoked.

On the selection of the second option takeVideoFromCamera() method is called.

Below is the code for chooseVideoFromGallery method.

Method is very simple. One intent is created which will allow user to pick video from all the videos which are available on the android device.

Following is the code structure for takeVideoFromCamera method.

I have also defined one intent here, which will allow user to record the video from the camera.

After choosing video from the gallery or taking video from the camera, compiler will call the onActivityResult() method.

Both method contains the request code. Request code for gallery is defined by the constant GALLERY(value is 1) and for camera(value is 2) it is CAMERA.

Here is the lines of code for onActivityResult method.

In this method compiler will check the scenario whether the video is coming from the gallery or camera using the request code.

Different Request Codes are there for both intents as we have seen earlier.

If the video is selected from the gallery, then compiler will go into the if(requestcode == GALLERY) condition otherwise in to the else if(requestcode == CAMERA) condition.

We have URI of the video at this level.

URI contains the data structure of the video.

To save the video on the internal storage we require path to the video.

Path to the video can be retrieved from the URI using getPath() method.

saveVideoToInternalStorage() method will save the video. For using this method we need to pass the path to the video in it’s parameter.

After getting the path and saving the video, compiler will start the video into the videoview.

So that was all the useful details for android pick video from gallery or camera example tutorial.

Hope you have found out what you were looking for.

If not then comment out, we will try to make new tutorial.

Download code for android pick video from gallery or camera tutorial

[sociallocker]Click Here To Download SelectVideoGalleryCamera Source Code[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

4 Responses

  1. ram says:

    after pick the video from gallery how to compress video and how to send to video to server

  2. Abel says:

    what is the vv in xm?

  3. Abhishek pandey says:

    How can i give the fix path and player play the all video from folder ,kindly help me

Leave a Reply

Your email address will not be published. Required fields are marked *