4

I have a pretty simple implementation of FileObserver:

        observer = new FileObserver(rootPath+"Pictures") {
        @Override
        public void onEvent(int event, String path) {
            //event &= FileObserver.ALL_EVENTS;
            Log.d("home","event: "+event);
            if (event == FileObserver.CREATE) {
                Toast.makeText(cont, "File created", Toast.LENGTH_SHORT).show();

            }
        }
    };
    observer.startWatching();

it's not registering the FileObserver.CREATE constant (256). My log is showing several 1's (Access), a few 16's (close_nowrite) and a few 32's (open) when I create a file but never a 256.

I've tried on two devices (Samsung Galaxy S7 Active and One Plus Two)

anything I need to do differently here?

KrishnaJ
  • 707
  • 1
  • 7
  • 23
Blair Holmes
  • 1,305
  • 1
  • 15
  • 28
  • That you can display a Toast there!? I had to put it in runInUIThread(). I advise you to put an else statement there and show a toast with event and path. You could miss some events wich you did not expect. I put the observer on DCIM/Camera and get very many toasts after taking a picture. Including "File created". `path is a relative path: only the file name.` – greenapps Aug 19 '16 at 12:53
  • well that's not the issue. I've also put a Log.d() statement inside my if and it never gets called. I'm logging every event also and never see the event code for CREATE. – Blair Holmes Aug 19 '16 at 12:57
  • Could you try for DCIM/Camera too? `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+ "/Camera" ) ` – greenapps Aug 19 '16 at 12:58
  • If you create a file then how? Your own app or how? – greenapps Aug 19 '16 at 13:00
  • DCIM doesn't work either. I'm not going to be creating files in my app directly. I need to listen for files being added to the directory by a different process. – Blair Holmes Aug 19 '16 at 13:05
  • `DCIM ` .I ment DCIM/Camera and then take pictures. – greenapps Aug 19 '16 at 13:07
  • ya did that. still not registering CREATE – Blair Holmes Aug 19 '16 at 13:13
  • Maybe try different device? Other OS version? The CREATE is for a ...jpg.tmp file as first event. Is 128 a rename? – greenapps Aug 19 '16 at 13:15
  • Toast.makeText can work in main UI thread only, it should not be used here. – Leon Oct 10 '17 at 01:03

2 Answers2

0

I don't have the exact answer, but I know that there are more event codes than are listed in the doc and/or some of them are kind of combinations, especially when you are watching directories and subdirectories.

What you can do is, if you are interested in CREATE events, instead of asking

if (event == FileObserver.CREATE) { /* do something */ }

try asking if it contains CREATE flag:

if ((event & FileObserver.CREATE) == FileObserver.CREATE) { /* do something */}

In particular, though this is probably not in your case, when creating a directory, an ISDIR flag is added to the CREATE event.

Hope that helps someone. Here is a nice description of the system Android's FileObserver is based on.

alr3000
  • 63
  • 7
0

#Try this one, hope it will work for you. I already tested it.

observer = new FileObserver(rootPath+"Pictures", FileObserver.CREATE) {
        @Override
        public void onEvent(int event, String path) {
            //event &= FileObserver.ALL_EVENTS;
            Log.d("home","event: "+event);
            Toast.makeText(cont, "File created", Toast.LENGTH_SHORT).show();

            }
        }
    };
    observer.startWatching();

Make sure that rootPath + "Pictures" is the proper file path.