我有以下方法,它适用于Android 4.3或更早版本的内部存储和外部SD卡:
public void Recorder_Prepare() {
recorder = new MediaRecorder();
recorder.setPreviewdisplay(holder.getSurface());
mCamera.unlock();
recorder.setCamera(mCamera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(camcorderProfile);
recorder.setVideoSize(ResolutionWidth, ResolutionHeight);
File DirectoryExistCheck = new File(VideoFolderAbsolutePATH);
if(!DirectoryExistCheck.exists()) {
DirectoryExistCheck.mkdir();
}
String VideoPath = VideoFolderAbsolutePATH + separator + "Video.mp4";
File NewVideoFile = new File(VideoPath);
recorder.setoutputFile(NewVideoFile.getAbsolutePath());
recorder.setVideoFrameRate(camcorderProfile.videoFrameRate);
try {
recorder.prepare();
}
catch (IllegalStateException e) {
Log.e("mytag", "IllegalStateException : " + Log.getStackTraceString(e)); }
catch (IOException e) {
Log.e("mytag", "IOException : " + Log.getStackTraceString(e)); }
recorder.start();
}
........
........
现在我正在使用适用于Android 5.0的新存储访问框架选择一个文件夹:
public void FolderSelection_Lollipop() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, PICK_FOLDER_CODE_Lollipop);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if(requestCode == PICK_FOLDER_CODE_Lollipop) {
if(resultCode == RESULT_OK) {
// Get Uri from Storage Access Framework.
Uri Uri_Lollipop = resultData.getData();
VideoFolderAbsolutePATH = Uri_Lollipop.getPath();
//also tried:
VideoFolderAbsolutePATH = Uri_Lollipop.toString();
// Persist access permissions.
this.getContentResolver().takePersistableuriPermission(Uri_Lollipop,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
}
Recorder_Prepare()方法没有用,因为它在Android 5.0上给我IllegalStateException