Record Video in Android using Camera Intent
We can record video in android using camera intent.An intent action type of MediaStore.ACTION_VIDEO_CAPTURE or MediaStore.ACTION_IMAGE_CAPTURE can be used to capture images or videos or without directly using the Camera object.
Create Android Project with name as "StartVideo" with package name "com.etr.startvideo". Create Activity with name as "VideoActivity" and it will start video record. This is the code for VideoActivity.
Paste below code to "activity_video.xml"
Paste below code to "AndroidManifest.xml"
You can download source code here.
Create Android Project with name as "StartVideo" with package name "com.etr.startvideo". Create Activity with name as "VideoActivity" and it will start video record. This is the code for VideoActivity.
package com.etr.startvideo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class VideoActivity extends Activity {
public static final int code = 1111;
Button start_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
start_video = (Button) findViewById(R.id.start_video);
start_video.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivityForResult(new Intent(
MediaStore.ACTION_VIDEO_CAPTURE), code);
}
});
} catch (Exception e) {
Log.v("Camera Exception ", Log.getStackTraceString(e));
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Video Recorded", Toast.LENGTH_LONG)
.show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video, menu);
return true;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class VideoActivity extends Activity {
public static final int code = 1111;
Button start_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
start_video = (Button) findViewById(R.id.start_video);
start_video.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivityForResult(new Intent(
MediaStore.ACTION_VIDEO_CAPTURE), code);
}
});
} catch (Exception e) {
Log.v("Camera Exception ", Log.getStackTraceString(e));
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Video Recorded", Toast.LENGTH_LONG)
.show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video, menu);
return true;
}
}
Paste below code to "activity_video.xml"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/start_video"
android:layout_width="wrap_content"
android:text="Start video"
android:layout_height="wrap_content" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/start_video"
android:layout_width="wrap_content"
android:text="Start video"
android:layout_height="wrap_content" />
</LinearLayout>
Paste below code to "AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.startvideo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.startvideo.VideoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.startvideo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.startvideo.VideoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download source code here.
Comments
Post a Comment