Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect file change using FileObserver on Android
private class PathFileObserver extends FileObserver{
static final String TAG="FILEOBSERVER";
/**
* should be end with File.separator
*/
String rootPath;
static final int mask = (FileObserver.CREATE |
FileObserver.DELETE |
FileObserver.DELETE_SELF |
FileObserver.MODIFY |
FileObserver.MOVED_FROM |
FileObserver.MOVED_TO |
FileObserver.MOVE_SELF);
public PathFileObserver(String root){
super(root, mask);
if (! root.endsWith(File.separator)){
root += File.separator;
}
rootPath = root;
}
public void onEvent(int event, String path) {
switch(event){
case FileObserver.CREATE:
Log.d(TAG, "CREATE:" + rootPath + path);
break;
case FileObserver.DELETE:
Log.d(TAG, "DELETE:" + rootPath + path);
break;
case FileObserver.DELETE_SELF:
Log.d(TAG, "DELETE_SELF:" + rootPath + path);
break;
case FileObserver.MODIFY:
Log.d(TAG, "MODIFY:" + rootPath + path);
break;
case FileObserver.MOVED_FROM:
Log.d(TAG, "MOVED_FROM:" + rootPath + path);
break;
case FileObserver.MOVED_TO:
Log.d(TAG, "MOVED_TO:" + path);
break;
case FileObserver.MOVE_SELF:
Log.d(TAG, "MOVE_SELF:" + path);
break;
default:
// just ignore
break;
}
}
public void close(){
super.finalize();
}
}
@twlkyao

This comment has been minimized.

Copy link

@twlkyao twlkyao commented Jan 10, 2014

This can only monitor the top directory, the FileObserver class is not recursive.

@Doctoror

This comment has been minimized.

Copy link

@Doctoror Doctoror commented Jan 19, 2014

Change to switch(event & FileObserver.ALL_EVENTS).

@KVMystery

This comment has been minimized.

Copy link

@KVMystery KVMystery commented Apr 23, 2017

Instead of using Log.d to log an event,I would like to write all events to a file. Please help.

@hendrawd

This comment has been minimized.

Copy link

@hendrawd hendrawd commented Jul 11, 2017

This has issue on Marshmallow and forward https://issuetracker.google.com/issues/37065227
But the developer team said it has fixed and will be available in a future build, although it's not clear when

@thanhbinh84

This comment has been minimized.

Copy link

@thanhbinh84 thanhbinh84 commented Aug 12, 2018

The event in onEvent() doesn't returns exact the number as doc, you need to wrap as below

public void onEvent(int event, String path) {

        if ((FileObserver.CREATE & event)!=0) {
            PUtils.log("CREATE: " + rootPath + path);
        } else if ((FileObserver.MODIFY & event)!=0) {
            PUtils.log("MODIFY: " + rootPath + path);
        } else if ((FileObserver.DELETE & event)!=0) {
            PUtils.log("DELETE: " + rootPath + path);
        } else if ((FileObserver.DELETE_SELF & event)!=0) {
            PUtils.log("DELETE_SELF: " + rootPath + path);
        } else if ((FileObserver.MOVED_FROM & event)!=0) {
            PUtils.log("MOVED_FROM: " + rootPath + path);
        } else if ((FileObserver.MOVED_TO & event)!=0) {
            PUtils.log("MOVED_TO: " + rootPath + path);
        } else if ((FileObserver.MOVE_SELF & event)!=0) {
            PUtils.log("MOVE_SELF: " + rootPath + path);
        } else {
            PUtils.log("Unhandled event " + event + rootPath + path);
        }
@sagar-itpc

This comment has been minimized.

Copy link

@sagar-itpc sagar-itpc commented Apr 12, 2020

Getting this error in logcat
java.lang.NoSuchMethodError: No direct method (Ljava/io/File;I)V in class Landroid/os/FileObserver; or its super classes (declaration of 'android.os.FileObserver' appears in /system/framework/framework.jar)
Can anyone help

@hannesa2

This comment has been minimized.

Copy link

@hannesa2 hannesa2 commented Apr 22, 2020

Getting this error in logcat
java.lang.NoSuchMethodError: No direct method (Ljava/io/File;I)V in class Landroid/os/FileObserver; or its super classes (declaration of 'android.os.FileObserver' appears in /system/framework/framework.jar)
Can anyone help

I run into the same. Did you solved it ?

@TheBestDenis

This comment has been minimized.

Copy link

@TheBestDenis TheBestDenis commented Apr 22, 2020

Getting this error in logcat
java.lang.NoSuchMethodError: No direct method (Ljava/io/File;I)V in class Landroid/os/FileObserver; or its super classes (declaration of 'android.os.FileObserver' appears in /system/framework/framework.jar)
Can anyone help

I run into the same. Did you solved it ?

I saw that message (or something like that) when i was trying to use constructor FileObserver(File). Use of deprecated FileObserver(String) solved my problem. Don't know why...
And for a while look at FixedFileObserver. It's in russian, but code is "in" java. Original FileObserver has bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment