According to the documentation of onEvent()
:
The path, relative to the main monitored file or directory, of the file or directory which triggered the event
So I guess when path
is null
it is the the specified file or directory...
You need to keep track of the original path yourself. And append the path
of onEvent()
to this path to get the full path (unless you are tracking a file and its value is always null
):
FileObserver observer = new FileObserver(imageUri.getPath()) {
public String basePath;
@Override
public void onEvent(int event, String path) {
String fullPath = basePath;
if(path != null) {
// Eventually add a '/' in between (depending on basePath)
fullPath += path;
}
Log.d(TAG, "FILE: "+fullPath);
}
};
observer.basePath = imageUri.getPath();
observer.startWatching();
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 the basePath
and are not required to access the public field from outside the class/instance!