android - 如何通过 ACTION_VIDEO_CAPTURE 操作将文件输出设置为 mp4?

标签 android file android-intent camera mp4

当我使用我的 native 应用相机拍摄视频时,输出文件的扩展名为 3gp。我需要使用 ACTION_VIDEO_CAPTURE Intent 操作来拍摄相机,这将生成一个文件扩展名为 mp4 的文件。我该怎么做?

最佳答案

您可以继续尝试 dis 代码:

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

捕获完成后调用

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 数据) {

    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

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 格式保存捕获的视频。

关于android - 如何通过 ACTION_VIDEO_CAPTURE 操作将文件输出设置为 mp4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883934/

相关文章:

android - SyncAdapter 同步自定义应用程序数据?

php - 如何将变量名添加到文件末尾?

android - queryIntentActivities() 和 resolveActivity() 之间有什么区别。哪一个是了解现有应用程序的最佳方法?

Java,类的多个实例重置为最新创建的实例

java - 在触摸另一个 EditText 后请求焦点到一个 EditText?

php - 如何将 BackgroundWorker 的结果设置为 TextView

python 3 : Creating files in relative directories

c++ - 读取文本文件时 seekg() 的奇怪偏移

android - Google 通知中的语音转文本

android - Android语音识别器可以返回多个匹配项吗?