0

Hi i have been trying to record and save a video from Nougat 7.0 using intent, I can record a video but its not getting saved in the device storage. I even used FileProvider to avoid 'FileUriExposedException'. But when it comes for Capturing Images it is getting saved in the below specified path. Here is my code.

    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()) {

                }
        }
    }

The above code works well for all the pre-Nougat version. Can anyone provide me a better solution to record the video and save in device storage.

Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37
  • Please explain, in detail, what "its not working for video" means. Bear in mind that support for `content` `Uri` values for `EXTRA_OUTPUT` is up to the particular activity that responds to your `ACTION_VIDEO_CAPTURE` `Intent`. Some camera apps will support `content `Uri` values. Others will not. Google's own camera app did not support it until this summer, for example. – CommonsWare Nov 23 '16 at 18:24
  • @CommonsWare I meant i can get the camera image preview using the above method by replacing `MediaStore.ACTION_VIDEO_CAPTURE` with `MediaStore.ACTION_IMAGE_CAPTURE` but when it comes to video the preview is not working. Even when i tried to retrieve the video from its file path the file doesn't even exist. Sorry for the late reply :) – Jeffrey Rajan Nov 27 '16 at 08:19
  • What does "the preview is not working" mean? If you mean that the camera app is breaking, this is not surprising, though it is disappointing. – CommonsWare Nov 27 '16 at 13:11

2 Answers2

1

here is the solution !

If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.

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

also you should check the required permission android.Manifest.permission.CAMERA

Community
  • 1
  • 1
Frank
  • 2,738
  • 2
  • 14
  • 19
0
    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();
            }
}
Umer Waqas
  • 1,794
  • 1
  • 12
  • 15