I am using FileObserver to observe a directory for changes. The process works fine 90% of the time, but occasionally it fails.
Here is a working example of logcat:
04-23 21:12:03.873: V/ItemObserver(1663): Setting up new item observer for item 2
04-23 21:12:04.374: I/ItemObserver(1663): Received item event for item 2, event: 256, file: batch.get.47
04-23 21:12:07.866: I/ItemObserver(1663): Received item event for item 2, event: 512, file: batch.get.47
04-23 21:12:07.873: I/ItemObserver(1663): Received item event for item 2, event: 512, file: item.xml
04-23 21:12:07.883: I/ItemObserver(1663): Received item event for item 2, event: 256, file: item.xml
04-23 21:12:08.033: I/ItemObserver(1663): Received item event for item 2, event: 8, file: item.xml
Here is a failed example:
04-23 22:08:09.403: V/ItemObserver(1751): Setting up new item observer for item 2
04-23 22:08:09.813: I/ItemObserver(1751): Received item event for item 2, event: 256, file: batch.get.52
04-23 22:08:09.954: I/ItemObserver(1751): Received item event for item 2, event: 32768, file: null
Once I get the 32768 event with a null file, everything stops. I've checked the source for FileObserver and searched for inotify 32768 and can't find where this is referenced anywhere.
The code to set up the observer is as follows:
itemDirObserver = new FileObserver(getItemsCache().getProcessedItemDir(itemId).getPath(),
FileObserver.CLOSE_WRITE | FileObserver.CREATE | FileObserver.DELETE) {
@Override
public void onEvent(int event, final String file) {
itemDirChanged(event, file);
}
};
itemDirObserver.startWatching();
The code for the logcat is:
public synchronized void itemDirChanged(int event, String file) {
Log.i(LOG, "Received item event for item " + itemId + ", event: " + event + ", file: " + file);
switch (event) {
<snip>
Any idea what 32768 and null file signifies?
Thanks to this answer.
The event codes are listed here.
32768 in particular is this:
#define IN_IGNORED 0x00008000 /* File was ignored */
I too was suffering from occasional failures.
I discovered a big gotcha with the Android FileObserver: you mustn't have two FileObservers watching the same folder in your application.
If you call StopWatching
on one FileObserver, any other FileObserver that is watching the same folder will also stop watching.
I had the problem that my FileObserver gets 32768 event and stops working. I desperately tried to understand how to fix this (without recreating the FileObserver) for a couple of days.
First I found that although I have a hard reference to my FileObserver this event (32768) can be triggered by garbage collection (when I forced it through DDMS).
Eventually I found that there was another FileObserver in my program to the same folder. As soon as I deleted it everything started working.
Do anybody knows if it is legal to have several observers to the same directory? I couldn't find any information about it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With