牛轧糖中的视频捕捉?

时间:2017-04-07 06:15:23

标签: android video video-capture android-7.0-nougat

我是支持nougat的创建视频捕获应用程序,但我正在捕获视频,该视频已捕获并保存到默认的相机文件夹,但结果代码为-1。我正在使用以下代码。

public void startRecordingVideo() {
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        File mediaFile = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/myvideo.mp4");
        videoUri = Uri.fromFile(mediaFile);
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        startActivityForResult(intent, VIDEO_CAPTURE);
    } else {
        Toast.makeText(this, "No camera on device", Toast.LENGTH_LONG).show();
    }
}

我在这里放置了我的onActivityResult代码

if (requestCode == VIDEO_CAPTURE) {
            //if (resultCode == RESULT_OK) {
                int width = 0, DisplayWidth = 0;
                //int height = 0, DisplayHeight = 0;
                if (new File(Constants.APP_PATH + "/myvideo1.mp4").exists()) {
                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                    retriever.setDataSource(Constants.APP_PATH + "/myvideo1.mp4");
                    Bitmap bmp = retriever.getFrameAtTime();
                    width = bmp.getHeight();

                    VideoPath = Constants.APP_PATH + "/myvideo1.mp4";
                    DisplayMetrics displaymetrics = new DisplayMetrics();
                    MainActivity.this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                    DisplayWidth = displaymetrics.widthPixels;
                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    int VideoDuration = Integer.parseInt(time);
                    if (width >= DisplayWidth * 1.5 && VideoDuration <= Constants.VIDEO_UPPER_LIMIT) {
                        new ResizeVideoTask(VideoPath).execute();
                    } else if (VideoDuration > Constants.VIDEO_UPPER_LIMIT) {
                        showTimeDialog();
                    } else {
                        File afile = new File(VideoPath);
                        String path = Constants.APP_PATH + "/myvideo.mp4";
                        File destfile = new File(path);
                        if (destfile.exists()) {
                            destfile.delete();
                        }
                        copyFile(new FileInputStream(afile),new FileOutputStream(destfile));
                        Intent switchIntent = new Intent(MainActivity.this,VideoActivity.class);
                        switchIntent.putExtra("URI", Uri.fromFile(destfile).toString());
                        switchIntent.putExtra("FILE_PATH", path);
                        startActivity(switchIntent);
                    }
                    retriever.release();
                }
            } else if (resultCode == RESULT_CANCELED) {

            } else {

            //}
        }

1 个答案:

答案 0 :(得分:0)

首先,您未在EXTRA_OUTPUT ACTION_VIDEO_CAPTURE中传递Intent。因此,相机应用会选择存储视频的位置,并通过发送至Uri的{​​{1}}返回Intent。你没有使用它。

其次,如果您取消注释onActivityResult()行(以解决前一段中的问题),那么您在此处使用的intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);的定义与您在{{1}中使用的任何内容的定义不同}}