androidfileobserver

Retrieve new and old path of a renamed file under observing directory


My App saves recorded video files in a specific folder in external directory and saves meta data including the path of every file in database . I need to Observe remove , rename and move of my file in case of keeping database updated with new file path. I read about FileObserver in android. If I give the path of my directory in SD Card for example /storage/emulated/0/my_recordings as input path of my FileObserver, events will be fired for all of my files in the input path and also it will be fired for MOVED_TO event type which i use to observe rename operation of files under my directory, I will get the new relative path of my file in the path argument of onEvent (int event, String path) but how can I retrieve the old file name or old file path of that file to understand which database entry I have to update ? I passed the path of parent directory of that file so I don't have the old file name.

Thanks in advance


Solution

  • There should be a MOVED_FROM event right before the MOVE_TO. The path of the MOVED_FROM event should be the old file name while the path in the MOVED_TO should contain the new file name.

    Also make sure that you monitor this event type by explicitly passing it in the flags parameter of the FileObserver constructor.