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;
}
}
You have hard-coded output destination.
As an option you can add logic to your getFilePath() method to manage file names.