【问题标题】:MediaStore.ACTION_VIDEO_CAPTURE not saving the video in Nougat 7.0MediaStore.ACTION_VIDEO_CAPTURE 不在 Nougat 7.0 中保存视频
【发布时间】:2016-11-23 18:22:33
【问题描述】:

您好,我一直在尝试使用 Intent 从 Nougat 7.0 录制和保存视频,我可以录制视频,但它没有保存在设备存储中。我什至使用FileProvider 来避免'FileUriExposedException'。但是当涉及到捕获图像时,它会保存在下面的指定路径中。 这是我的代码。

    private Uri imageUri;
    private File imageFile = null;
    public File videoFilePath() {
            return new File(getDefaultCameraPath(), "Video_" + System.currentTimeMillis() + ".mp4");
        }
private void callCameraIntent(){
    Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    imageFile = videoFilePath(); 
    imageUri = FileProvider.getUriForFile(CreatePostActivity.this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(cameraIntent, 2);
}
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (resultCode != Activity.RESULT_CANCELED) {
            //Here it returns imageFile does not exist so it skips the if statement
            if (resultCode == Activity.RESULT_OK && requestCode == 2 && imageFile != null && imageFile.exists()) {

                }
        }
    }

以上代码适用于所有pre-Nougat 版本。谁能给我一个更好的解决方案来录制视频并保存在设备存储中。

【问题讨论】:

  • 请详细解释“它不适用于视频”是什么意思。请记住,对EXTRA_OUTPUT 的content Uri 值的支持取决于响应您的ACTION_VIDEO_CAPTURE Intent 的特定活动。一些相机应用程序将支持content Uri` 值。其他人不会。例如,谷歌自己的相机应用直到今年夏天才支持它。
  • @CommonsWare 我的意思是我可以使用上述方法通过将MediaStore.ACTION_VIDEO_CAPTURE 替换为MediaStore.ACTION_IMAGE_CAPTURE 来获得相机图像预览,但是当涉及到视频时,预览不起作用。即使我试图从其文件路径中检索视频,该文件甚至都不存在。抱歉回复晚了:)
  • “预览不起作用”是什么意思?如果您的意思是相机应用程序出现故障,这并不奇怪,尽管令人失望。

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


【解决方案1】:

这里是解决方案!

如果您的 targetSdkVersion 为 24 或更高,我们必须使用 FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用程序可以访问它们。

Stackoverflow 链接: android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

你也应该检查所需的权限 android.Manifest.permission.CAMERA

【讨论】:

  • 那么,解决方案是什么?我有同样的问题。
【解决方案2】:
    File Video_folder = new File(Environment.getExternalStorageDirectory(),
 "Sample/Videos")
File videoMediaFile=null;
    private void DispatchVideoRecordIntent() {
            try {
                videoMediaFile = File.createTempFile(
                        "VID_" + System.currentTimeMillis(),
                        ".mp4",
                        Video_folder
                );
                Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoMediaFile));
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            } catch (Exception e) {
                Log.e("Image Capturing", e.toString());
            }
        }



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {
            if (!TextUtils.isEmpty(videoMediaFile.getAbsolutePath())) {
                       // videoMediaFile.getAbsolutePath() is your file path

            } else {
                Toast.makeText(this, "Unable to capture video.", Toast.LENGTH_SHORT).show();
            }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    相关资源
    最近更新 更多