According to the documentation,
"Each FileObserver instance monitors a single file or directory. If a directory is monitored,
events will be triggered for all files and subdirectories inside the monitored directory."
My code goes like,
FileObserver fobsv = new FileObserver("/mnt/sdcard/") {
@Override
public void onEvent(int event, String path) {
System.out.println(event+" "+path);
}
};
fobsv.startWatching();
However, the onEvent()
is triggering only when a file is changed in the /mnt/sdcard/. If I create a file in /mnt/sdcard/downloads/, the method is not getting fired.
Is there any problem with the code?
The documentation is incorrect, as is noted in this issue.
No, but
FileObserver
is not recursive, despite the documentation to the contrary.There is an open-source
RecursiveFileObserver
that works just as the normalFileObserver
should ... I am using it currently it is what it is named , it acts as a FileObserver that is recursive for all directories beneath the directory you chose ...Here is it :
Make a new class in your app and copy this code to it , and use it as you like ! Vote up if you find this helpful !