android video capture example using camera

Download Android video capture example using camera

Post on 24-Jan-2017




0 download

Embed Size (px)



    Introduction:If we want to use the inbuilt Camera of our device by your application then we have to initiate the Intent for it.By using android.provider.MediaStore class we can easily capture the video.The device Camera application requests video capture using android.provider.MediaStore.ACTION_VIDEO_CAPTURE.To launch the camera there is a need to initiate the intent by using startActivity(..) or startActivityForResult(..) method.startActivity() and startActivityForResult() with Camera Video Capturing:

    startActivity():If we want to launch the Camera application without worrying about the result of the captured video,then we can call this method:Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);startActivity(captureVideoIntent);startActivityForResult():Here, to access the captured video from a camera activity, the camera intent must be initiated using this call. It not only allows to start the camera application, but also one can also expect to get a result in form of a captured video.Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);startActivityForResult(captureVideoIntent,VIDEO_CAPTURED);To access the captured video we will override the callback method onActivityResult().Here we get the data and compare the request code, which is the second argument of the startActivityForResult() method, that the video has captured.Example with Code:Lets see how we can create the Camera video capturing example by using startActivity() and startActivityForResult() method.


    STEPS:1.Create new Application CameraVideoCapturingExample.AndroidManifest.xml:First, we have to add some permissions to create the Camera application.Add this in your manifest file.activity_main.xml:Inside this, we add the three buttons and a video view.

  • ACADGILDACADGILD1st button is for capturing the video by expecting a result, which means it uses startActivityForResult().2nd button is for only capturing the video without accessing the video, which meansit uses startActivity().3rd is to play the captured video. There is a condition that, if we capture the video by using startActivityForResult() then the play video button is enabled otherwise not.VideoView is used to play the video that has been we will be using the VideoView object with Uri to play the captured video.VideoView videoView;UrivideoFileUri;public static int VIDEO_CAPTURED = 1;It is a constant which is used as a request code in onActivityResult() method.Add some code in onCreate(){}Take the reference of all buttons and video views and set the listeners on buttons.captureVideoButton = (Button) this.findViewById(;playVideoButton = (Button) this.findViewById(;captureWithoutDataVideoButton = (Button) this.findViewById(;videoView = (VideoView) this.findViewById(;captureVideoButton.setOnClickListener(this);playVideoButton.setOnClickListener(this);captureWithoutDataVideoButton.setOnClickListener(this);Initially, playVideoButton is not enabled. We will set enable when we capture the video.playVideoButton.setEnabled(false);Inside onClick() we can do this like:if (v == captureVideoButton) {


    Intent captureVideoIntent =new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);startActivityForResult(captureVideoIntent,VIDEO_CAPTURED);}if (v == captureWithoutDataVideoButton) {playVideoButton.setEnabled(false);Intent captureVideoIntent =new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);startActivity(captureVideoIntent);}else if (v == playVideoButton) {videoView.setVideoURI(videoFileUri);videoView.start();}And our captured video will be accessed through onActivityResult() methodAdd some code in onActivityResult() method:if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {videoFileUri = data.getData();playVideoButton.setEnabled(true);}Output:Before Capture Video

    After Capture Video


    While Play Video

    Conclusion:This is the basic idea of how to create a similar camera application. We can customize it as per the need of the application and make it even better. It only shows how to launch the camera activity and get the data in return to capture the video. It finds usage in popular applications like Instagram. The camera functionality plays an important role in many applications and it is very popular in Android development.



View more >