Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the output of a file as mp4 using intent to a camera with a action of ACTION_VIDEO_CAPTURE?

when I am using my native app camera to take video, the output file have 3gp extension. I need to intent to camera with ACTION_VIDEO_CAPTURE intent action which will produce a file that has a mp4 file extension. how can I do it?

like image 232
Earwin delos Santos Avatar asked Nov 22 '22 04:11

Earwin delos Santos


1 Answers

You can go ahead and try dis code:

intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);    
   fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO);  // create a file to save the video in specific folder (this works for video only)
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    // start the Video Capture Intent
    startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);

This will be called once the capture is completed

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
    case REQUEST_VIDEO_CAPTURED_NEXUS:
    this.videoFromCamera(resultCode, data);
    break;

default:
                break;
            }
        }
    }

private void videoFromCamera(int resultCode, Intent data) {

    if(fileUri != null) {
        Log.d(TAG, "Video saved to:\n" + fileUri);
        Log.d(TAG, "Video path:\n" + fileUri.getPath());
        Log.d(TAG, "Video name:\n" + getName(fileUri)); 
// use uri.getLastPathSegment() if store in folder
//use the file Uri.
    }
}

Get the output Media file uri with the following Method

public Uri getOutputMediaFile(int type)
    {
        // To be safe, you should check that the SDCard is mounted

        if(Environment.getExternalStorageState() != null) {
            // this works for Android 2.2 and above
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");

            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.

            // Create the storage directory if it does not exist
            if (! mediaStorageDir.exists()) {
                if (! mediaStorageDir.mkdirs()) {
                    Log.d(TAG, "failed to create directory");
                    return null;
                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
           if(type == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ timeStamp + ".mp4");
            } else {
                return null;
            }

            return Uri.fromFile(mediaFile);
        }

        return null;
    }

This will save the captured video in pure MP4 format.

like image 97
Vaibhav Barad Avatar answered Dec 26 '22 19:12

Vaibhav Barad