我正在开发一个Android应用程序,我正在通过原生视频录制视频,代码如下。现在我想限制视频大小不超过10M,就像在视频中超过限制录制时应该停止。
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
答案 0 :(得分:4)
您需要在启动录制时再添加一个参数MediaStore.EXTRA_SIZE_LIMIT,
即:
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//add one more parameter
long maxVideoSize = 10*1024*1024; // 10 MB
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
答案 1 :(得分:0)
您可以在录制时连续检查文件的大小,并在超出大小时将其停止 -
public long TotalMemory()//Environment.getExternalStorageDirectory().getAbsolutePath()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long Total = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize()) / 1048576;
return Total;
}
public long FreeMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize()) / 1048576;
return Free;
}
public long BusyMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long Total = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize()) / 1048576;
long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize()) / 1048576;
long Busy = Total - Free;
return Busy;
}
希望这可以帮助你:)