0

我正在使用 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 的(录像的)路径是否为空,但不存在这个问题,并且日志显示了正确的视频路径名。为什么?

4

1 回答 1

0

由于这是要在 google 中显示的最新问题,因此我在此处添加我的输入:

可能的问题1:

给观察者的路径错误,缺少/或文件不存在

可能的问题2:

缺少对路径的授予访问权限,例如 android.permisison.READ_STORAGE 或未按要求使用 FileProvider

可能的问题3:

android 6 fileObserver bug,它不起作用,唯一的解决方法是做一个每X秒检查一次的looper

于 2018-06-05T12:32:04.373 回答