Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileObserver is called twice

I'm using a FileObserver to watch a folder in android. When a user modifies or creates a new file in this folder, the FileObserver should do some stuff with this file.

The thing is that with the use of the clause FileObserver.MODIFY, every time I create/modify a file in this watched folder, the FileObserver method onEvent() is called twice. This is a problem for me because it ruins everything I do afterwards (it's done twice).

This is my code:

mFileObserver = new FileObserver(directoryPath, FileObserver.MODIFY){
        public void onEvent(int event, String fileName){
            if (event == FileObserver.MODIFY){
                // some stuff to do
            }
        }
    }; 
like image 838
Fernando Avatar asked Nov 23 '22 09:11

Fernando


1 Answers

FileObserver.CLOSE_WRITE is triggered when a file that was just written to is closed. You just need to check for that event only.

public void onEvent(int event, String fileName){
        if (event == FileObserver.CLOSE_WRITE){
            // some stuff to do
        }
    }
like image 189
clementiano Avatar answered Nov 25 '22 00:11

clementiano