我正在使用MediaRecorder在我的相机应用中创建视频;在“准备方法”中,我使用recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
设置输出文件,而getOutputMediaFile方法是以下
private void observeVideo(String mediaFilePath){
observer = new FileObserver(mediaFilePath, FileObserver.CLOSE_WRITE) {
@Override
public void onEvent(int event, String path) {
stopWatching();
Toast.makeText(context, path + " saved correctly", Toast.LENGTH_LONG);
}
};
observer.startWatching();
}
private File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
return null;
}
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
observeVideo(mediaFile.toString());
} else {
return null;
}
return mediaFile;
}
问题是没有调用onEvent。我检查了传递给FileObserver的路径(录制的视频)是否为空,但没有这个问题,Log显示了正确的视频路径名。为什么呢?
答案 0 :(得分:0)
由于这是谷歌中显示的最新问题,我在这里添加我的输入:
给观察者的路径是错误的,缺少/或文件不存在
缺少对android.permisison.READ_STORAGE等路径的授予访问权限,或者根本不使用FileProvider
android 6 fileObserver bug,它不会工作,唯一的解决方法是做一个每X秒检查一次的活套