0

I'm using MediaRecorder to create videos in my Camera app; in the "prepare method" I set the output file with recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); while the getOutputMediaFile method is the following

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;
}

The problem is that onEvent isn't called. I checked if the path (of the video recordered) that I pass to the FileObserver is null, but there isn't this problem and Log show me the right pathname of video. Why?

sbomb
  • 1
  • 3

1 Answers1

0

Since this is the most recent question of the ones to show in google i am adding my input in here:

possible problem 1:

the path given to the observer is wrong, missing a / or the file does not exist

possible problem 2:

missing grant access to the path like android.permisison.READ_STORAGE or its not using a FileProvider as required

possible problem 3:

android 6 fileObserver bug, it wont work and the only work around is to do a looper that check every X seconds

Yosef
  • 1,161
  • 15
  • 18