Sunday, April 10, 2016

Video Record and Saved in External Storage Device

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of Video Recording using camera App .
Video will be stored at your specified path.

Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
***** check on physical device
Suggestion - Add Features and permissions in manifest file like   


<uses-feature android:name="android.hardware.camera2" />


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

A. xml file - only with one button 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.exam.ravi.videorecordex1.MainActivity">

    <Button
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Record Video"        android:id="@+id/button"

        android:onClick="recordVideo"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="54dp" />
</RelativeLayout>

B. Java File 

package com.exam.ravi.videorecordex1;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    File videoPath;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void recordVideo(View view) {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        String TEMP_DIR = Environment.getExternalStorageDirectory() + 
        File.separator + Calendar.getInstance().getTime() + ".mp4"
                            // Get File Path        videoPath = new File(TEMP_DIR);
        Uri vdUri = Uri.fromFile(videoPath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, vdUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, 1);
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (videoPath.exists()) {
                  Toast.makeText(this, "File saved path is " +
                          videoPath.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    } else {
                  Toast.makeText(this, "File Not saved ", Toast.LENGTH_LONG).show();

                    }
                case Activity.RESULT_CANCELED:
                    break;
            }
        }
    }
}

No comments:

Post a Comment