FileObserver -> onEvent(event, path): path is NULL
Asked Answered
P

2

6

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!

Prevot answered 19/2, 2013 at 8:49 Comment(0)
B
7

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!

Bouleversement answered 19/2, 2013 at 9:1 Comment(2)
So in which cases would the "path" be non-null, and what will its value mean in such cases?Brittnybritton
I suppose that when a file is moved (see MOVED_FROM, MOVED_TO, etc. events) path will contain the new location (relative to observed directory/or file, see my code snippet).Bouleversement
Y
4

I just encountered something like this today. I had a FileObserver monitoring a folder for new files, which I then attempted to do something with the downloaded images. When I went to access the images by BitmapFactory.decodeFile(ImgPath), I would get sometimes get a NULL result. This seemed to happen on newer and faster devices, and never in debug when stepping through the event. I came to the conclusion that the file was still in use or not completely finished yet and I had to wait until the system unleashed its claws from the file.

I'm new to Android development and am not familiar with the proper way to do this yet, but avoided the NULL issue for the moment by inserting a Thread.sleep. I know this is terrible, but it worked as a temp solution for me.

Yates answered 22/5, 2013 at 19:16 Comment(1)
The issue still persists, there is no nice way to handle this as far as I see it. Rather than stop and do the job on the main UI thread, rather use another thread, but it is still bad.Selfdiscipline

© 2022 - 2024 — McMap. All rights reserved.