Search code examples
androidvideo-capturemediastore

error calling MediaStore.ACTION_VIDEO_CAPTURE


i'm trying to create an app that allows video recording. i know that using MediaStore.ACTION_IMAGE_CAPTURE, it actually calls the camera system from my app and after taking the picture, it will return to my app with result.

while using the code, i found a MediaStore.ACTION_VIDEO_CAPTURE. which i assume it will camera but in video mode, rather then image capturing mode.

the code that i used for calling the camera in video mode:

Intent takeVideoFromCameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/vid_"+ String.valueOf(System.currentTimeMillis()) + ".mp4"));
takeVideoFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(takeVideoFromCameraIntent, RESULT_OK);

when i run the app with a real device, it does call the camera in video mode and also allows video recording. however, when i press the record button to finish recording, it returns to my app with a force close message saying that the camera is not responding.

at 1st, i thought that the video has not been captured, but when i searched for the file, it actually exist.

then, i thought its my onActivityResult method that causes the problem, but after i comment it with /* ... */ , it still have the same problem. but there isn't any details shown in LogCat.


Solution

  • i realize that i got the error because i'm adding extra to it. what i just needed to do is

    Intent takeVideoFromCameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    startActivityForResult(takeVideoFromCameraIntent, 1111);
    

    then, add an onActivityResult, with the request code == 1111 (depends on what you entered) and retrieve the last modified file that consist of the extension ".mp4" from the default folder of camera "DCIM/Camera"

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
    
        if(requestCode == 1111)//cam
        {
             File folder = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera");
             long folderModi = folder.lastModified();
    
        FilenameFilter filter = new FilenameFilter() 
        {
            public boolean accept(File dir, String name) 
            {
                return (name.endsWith(mp4));
            }
        };
    
        File[] folderList = folder.listFiles(filter);
    
        String recentName = "";
    
        for(int i=0; i<folderList.length;i++)
        {
            long fileModi = folderList[i].lastModified();
    
            if(folderModi == fileModi)
            {
                recentName = folderList[i].getName();
            }
        }
    }
    

    this way, i can get the name of the file and also do the modification (e.g renaming) with it.

    hope this would help other people. =)