如何设置文件使用意图的相机ACTION_VIDEO_CAPTURE的作用MP4的输出? [英] How to set the output of a file as mp4 using intent to a camera with a action of ACTION_VIDEO_CAPTURE?

查看:2779
本文介绍了如何设置文件使用意图的相机ACTION_VIDEO_CAPTURE的作用MP4的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用我的本机应用程序相机拍摄的视频,输出文件有3GP扩展。我需要意图的摄像头,这将产生具有MP4文件扩展名的文件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?

推荐答案

您可以继续尝试DIS code:

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;
            }
        }
    }

私人无效videoFromCamera(INT结果code,意图数据){

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.
    }
}

通过下面的方法输出媒体文件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;
    }

这将保存拍摄的视频在纯MP4格式。

This will save the captured video in pure MP4 format.

这篇关于如何设置文件使用意图的相机ACTION_VIDEO_CAPTURE的作用MP4的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆