Question

I am using built-in android intent in my camera app for video recording. My app can launch camera application and record video but as I click stop button of built-in camera app my app crashes and when check the directory where I save the videos, the recorded videos are stored there in the directory.

Here is my code please check it.

Button makeVideo = (Button) findViewById(R.id.makeVideo );
            makeVideo.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v) 
                {

                    Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);

                    Uri fileUri = getOutputMediaFileUri(); // create a file to save the video

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

                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                    startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);

                }
            });




    /** Create a file Uri for saving an image or video */
     private static Uri getOutputMediaFileUri() 
     {
         return Uri.fromFile(getOutputMediaFile());
     }

     /** Create a File for saving an image or video */
     private static File getOutputMediaFile() 
     {
         // To be safe, you should check that the SDCard is mounted
         // using Environment.getExternalStorageState() before doing this.

         File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath(), "My Videos");
         // 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("MyCameraApp", "failed to create directory");
                 return null;
             }
         }

         // Create a media file name
         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
         File mediaFile;
         mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
         return mediaFile;
     }


@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == RESULT_OK)
            {
                if (requestCode == REQUEST_VIDEO_CAPTURED) 
                {
                    uriVideo = data.getData();

                }
            }
    }

Here is my logcat enter image description here

Was it helpful?

Solution

The API can't handle the path what you gave at

mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");

try with:

mediaFile = new File(mediaStorageDir.getPath() + "VID_" + timeStamp + ".mp4");

maybe it will solve it or do a simpler time format:

mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_temp.mp4");

OTHER TIPS

mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");

File.sepator is system-depent. File has a constructor that takes two paramters, a File and a String:

mediaFile = new File(mediaStorageDir,  "VID_" + timeStamp + ".mp4");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top