PAdams
PAdams

Reputation: 67

Capturing and recording new video files in Android

I have an app that captures video and I've managed to get it to save the video file on the device gallery.

My only problem is that it just overwrites the previous file when I record a new video. I've tried to look for a solution, but can't find a way of recording a new video without overwriting the previous ones.

My class is:

public class CameraVideoActivity extends Activity {

private final int VIDEO_REQUEST_CODE = 100;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_video);
}

public void captureVideo(View view)
{
    Intent camera_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    File video_file = getFilepath();
    Uri uri = FileProvider.getUriForFile(CameraVideoActivity.this, 
    BuildConfig.APPLICATION_ID + ".provider",video_file);
    camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    camera_intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(camera_intent, VIDEO_REQUEST_CODE);
    sendBroadcast(camera_intent);

    MediaScannerConnection.scanFile(CameraVideoActivity.this, new String[]{video_file.toString()}, null,
            new MediaScannerConnection.OnScanCompletedListener() 
            {
                public void onScanCompleted(String path, Uri uri) 
                {
                    Log.i("External Storage", "Scanned" + path + ":");
                    Log.i("External Storage", "-> uri=" + uri);
                }
            });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == VIDEO_REQUEST_CODE)
    {
        if (resultCode == RESULT_OK)
        {
            Toast.makeText(getApplicationContext(), "Video Successfully Recorded", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Video Not Recorded", Toast.LENGTH_LONG).show();
        }
    }
}

public File getFilepath()
{
    File root=new 
    File(Environment.getExternalStorageDirectory(),"/RecordedVideo/");  
    //you can replace RecordVideo by the specific folder where you want 
     to save the video
    if (!root.exists()) 
      {
        System.out.println("No directory");
        root.mkdirs();
      }

    File video_file = new File(root, "sample_video.mp4");
    return video_file;
}
}

Upvotes: 0

Views: 123

Answers (2)

zeekhuge
zeekhuge

Reputation: 1594

In the getFilepath() method of your code, the File video_file = new File(root, "sample_video.mp4"); always returns the same file. Hence previous file gets overwritten.

What you can do is, use the current-epoch-time as the file name. That is, you can replace the statement

File video_file = new File(root, "sample_video.mp4");

with

File video_file = new File(root, "sample_video_" + System.currentTimeMillis() + ".mp4");

Thus each file would have a name in the format sample_video_<current_epoch_time>.mp4 and the last file will not be overwritte.

Upvotes: 1

You have hard-coded output destination.

As an option you can add logic to your getFilePath() method to manage file names.

Upvotes: 2

Related Questions