录制视频并保存在SD卡中

时间:2018-08-07 11:47:24

标签: android record android-external-storage

我拍了张照片,并将其保存到外部存储中:

var yAxes = pChart.yAxis;
yAxes.forEach(function(axis) {
    axis.labelGroup.element.childNodes.forEach(function(label){
        label.style.cursor = "pointer";
        label.onclick = function(){console.log('You clicked on '+this.textContent);};
    });
    axis.axisTitle.element.style.cursor = "pointer";
    axis.axisTitle.element.onclick = function(){console.log('You clicked on '+this.textContent);};
});

工作正常 但是录制视频呢
我写了一些这样的代码,但我认为这是不正确的:

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photoFile = getPhotoFile();

            String authorities = getActivity().getPackageName() + ".fileprovider";
            imageUri = FileProvider.getUriForFile(getActivity(), authorities, photoFile);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, 1);

我该怎么办?

编辑: 这些方法会创建一个目录并返回照片和视频文件

            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            File videoFile = getVideoFile();

            String authorities = getActivity().getPackageName() + ".fileprovider";
            videoUri = FileProvider.getUriForFile(getActivity(), authorities, videoFile);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
            startActivityForResult(intent, 2);

2 个答案:

答案 0 :(得分:0)

要录制视频,请使用:

startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);

这里是example

答案 1 :(得分:0)

在您的recordVideo.onClick()方法中添加此代码

 if (checkPermissions(YourActivity.this)) {
                captureVideo();
            } else {
                requestCameraPermission(MEDIA_TYPE_VIDEO);
            } 


public static boolean checkPermissions(Context context) {
    return ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == 
   PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, 
   Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, 
 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

//在CaptureVideo方法中

private void captureVideo() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    File file = getOutputMediaFile(MEDIA_TYPE_VIDEO);
    if (file != null) {
        imageStoragePath = file.getAbsolutePath();
    }

    Uri fileUri = getOutputMediaFileUri(getApplicationContext(), file);

    // set video quality
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file

    // start the video capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

/ **      *在打开相机之前创建并返回图像或视频文件      * /

 public static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            YourActivity.GALLERY_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e(YourActivity.GALLERY_DIRECTORY_NAME, "Oops! Failed create "
                    + YourActivity.GALLERY_DIRECTORY_NAME + " directory");
            return null;
        }
    }


 public static Uri getOutputMediaFileUri(Context context, File file) {
    return FileProvider.getUriForFile(YourActivity.this, context.getPackageName() + 
   ".provider", file);
}


 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Refreshing the gallery
            refreshGallery(getApplicationContext(), imageStoragePath);

            // video successfully recorded
           showVideo();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled recording
            Toast.makeText(getApplicationContext(),
                    "cancelled video recording", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}


public static void refreshGallery(Context context, String filePath) {
    // ScanFile so it will be appeared on Gallery
    MediaScannerConnection.scanFile(context,
            new String[]{filePath}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                }
            });
}


  private void showVideo() {
    try {

        videoView.setVideoPath(imageStoragePath);
        // video playing
        videoView.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }