I want to know when a file is finished writing, and to do that I’m trying to use FileObserver. I’m doing it like this:
FileObserver observer = new FileObserver(imageUri.getPath()) {
@Override
public void onEvent(int event, String path) {
if(event == FileObserver.CLOSE_WRITE)
Log.d(TAG, "FILE: "+path);
}
};
observer.startWatching();
imageUri is a valid Uri. When the file is closed I get the following log entry:
FILE: null
Why is it null? It’s possible that the user writes several files, so I need to know which one is triggering the event.
Thanks!
According to the documentation of
onEvent():So I guess when
pathisnullit is the the specified file or directory…You need to keep track of the original path yourself. And append the
pathofonEvent()to this path to get the full path (unless you are tracking a file and its value is alwaysnull):I tried to keep the example as close to your code snippet as possible. But, it is much better to create a full-blown class extending
FileObserver, so you can add an constructor to store thebasePathand are not required to access the public field from outside the class/instance!