我有一个可以抓取视频的应用,我设法让它将视频文件保存在设备库中。
我唯一的问题是它在录制新视频时会覆盖以前的文件。我试图找一个解决方案,但找不到录制新视频的方法而不会覆盖以前的视频。
我的课程是:
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;
}
}
答案 0 :(得分:2)
您有硬编码的输出目的地。
作为一个选项,您可以为getFilePath()方法添加逻辑来管理文件名。
答案 1 :(得分:1)
在代码的getFilepath()
方法中,File video_file = new File(root, "sample_video.mp4");
始终返回相同的文件。因此,先前的文件会被覆盖。
您可以使用current-epoch-time作为文件名。也就是说,您可以替换语句
File video_file = new File(root, "sample_video.mp4");
与
File video_file = new File(root, "sample_video_" + System.currentTimeMillis() + ".mp4");
因此,每个文件的格式都为sample_video_<current_epoch_time>.mp4
,最后一个文件不会被覆盖。