最近客户有一个视频录制的需求,于是,我就整理下可行的方案。
启动系统视频录制的代码:
Intent mIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.5);//画质0.5
mIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 70000);//70s
startActivityForResult(mIntent, CAMERA_ACTIVITY);//CAMERA_ACTIVITY = 1
系统返回后,在onActivityResult处理视频文件:
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != RESULT_OK)
return;
try {
AssetFileDescriptor videoAsset = getContentResolver()
.openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File tmpFile = new File(Environment.getExternalStorageDirectory(),
"recordVideo_0_5.3gp");
FileOutputStream fos = new FileOutputStream(tmpFile);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException io_e) {
// TODO: handle error
}
}
就这样,就可以调用系统的视频录制了,但别忘了必要的权限声明:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
kkk