Android Records a Video into SDCard
I am trying to record a video into sdCard. But everytime I click on the Button to record, it shows "stopped unexpectedly" error.开发者_JAVA百科 Not too sure where went wrong. I am not sure if setOutputFile's path is how I should indicate.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
cam = new cameraview(this);
((FrameLayout) findViewById(R.id.preview)).addView(cam);
// Create A Preview View
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(toogleButtonFlag )
{
startRecording();
toogleButtonFlag = false;
}
else{
stopRecording();
toogleButtonFlag = true;
}
}
});
}
public void startRecording(){
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setOutputFile("/sdcard/.3pg");
try{
recorder.prepare();
}
catch(IOException e)
{
e.printStackTrace();
recorder.reset();
recorder.release();
}
recorder.start(); // Recording is now started
}
public void stopRecording(){
recorder.stop();
recorder.reset();
recorder.release();
}
Make sure you have the
WRITE_EXTERNAL_STORAGE
permissionYour output file assumes the SD card is at
/sdcard
, which is incorrect on some devices and Android versions -- please useEnvironment.getExternalStorageDirectory()
Your output file lacks a file name
Your output file has a mis-spelled extension
精彩评论