12

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?

twlkyao
  • 13,208
  • 7
  • 24
  • 39
rahul
  • 5,837
  • 3
  • 25
  • 39

2 Answers2

35

There is an open-source RecursiveFileObserver that works just as the normal FileObserver 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 :

public class RecursiveFileObserver extends FileObserver {

public static int CHANGES_ONLY = CLOSE_WRITE | MOVE_SELF | MOVED_FROM;

List<SingleFileObserver> mObservers;
String mPath;
int mMask;

public RecursiveFileObserver(String path) {
    this(path, ALL_EVENTS);
}

public RecursiveFileObserver(String path, int mask) {
    super(path, mask);
    mPath = path;
    mMask = mask;
}

@Override
public void startWatching() {
    if (mObservers != null) return;
    mObservers = new ArrayList<SingleFileObserver>();
    Stack<String> stack = new Stack<String>();
    stack.push(mPath);

    while (!stack.empty()) {
        String parent = stack.pop();
        mObservers.add(new SingleFileObserver(parent, mMask));
        File path = new File(parent);
        File[] files = path.listFiles();
        if (files == null) continue;
        for (int i = 0; i < files.length; ++i) {
            if (files[i].isDirectory() && !files[i].getName().equals(".")
                && !files[i].getName().equals("..")) {
                stack.push(files[i].getPath());
            }
        }
    }
    for (int i = 0; i < mObservers.size(); i++)
        mObservers.get(i).startWatching();
}

@Override
public void stopWatching() {
    if (mObservers == null) return;

    for (int i = 0; i < mObservers.size(); ++i)
        mObservers.get(i).stopWatching();

    mObservers.clear();
    mObservers = null;
}

@Override
public void onEvent(int event, String path) {

}

private class SingleFileObserver extends FileObserver {
    private String mPath;

    public SingleFileObserver(String path, int mask) {
        super(path, mask);
        mPath = path;
    }

    @Override
    public void onEvent(int event, String path) {
        String newPath = mPath + "/" + path;
        RecursiveFileObserver.this.onEvent(event, newPath);
    } 

}
}

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 !

Seaskyways
  • 2,932
  • 3
  • 21
  • 37
  • 6
    Be careful: This is not completely recursive as it will not start watching automatically any newly created folder, nor will it stop watching folders that are moved out from the tree... Since this is licensed GPLv2, anybody solving these problems based on the current implementation should share back the code. – JM Lord May 20 '16 at 16:17
  • 1
    I used the code and it says "Can't create handler in a thread if Looper.create() is not called" :/ – Hasaan Ali May 21 '16 at 01:06
  • Please give credit/reference to the original source, I think that is: https://gist.github.com/gitanuj/888ef7592be1d3f617f6 – Yoav Feuerstein Jan 29 '19 at 14:27
  • 1
    @yoav-feuerstein, just a remark: Not sure why the original package declaration had been omitted in this answer (and unfortunately, the import statements, too). However, the class seems to be borrowed from `com.owncloud.android.utils`, as [another answer](https://stackoverflow.com/a/34239615/211514), which is also referring to this one over here, points out. The code similarity appears to be higher than to that gist. – klaus May 05 '20 at 13:14
8

According to the documentation

The documentation is incorrect, as is noted in this issue.

Is there any problem with the code?

No, but FileObserver is not recursive, despite the documentation to the contrary.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    FYI, the documentation as quoted above no longer mentions being recursive as it once mistakenly did, though it would be nice if it explicitly said it was not recursive - something the Linux man page for the underlying inotify does add in a note at the end. – Chris Stratton Dec 18 '13 at 17:16
  • What if I want to monitor the partition for storage changes ? Is there a way to do it? – android developer May 22 '15 at 16:11