1

I have a Service which starts a FileObserver to monitor pictures which are taken by the phone camera, in particular thefile :

String initialPictureFolder =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/Camera/"; 

by this function:

public void startFileObserver(String filePath){
    /* It only monitors this Folder, not any Subfolder */
    myFileOsberver = new PicturesFileObserver(filePath,UploadPhotosService.this);
    myFileOsberver.startWatching();
}

that said, the FileObserver only catches delete events (code 512) and nothing more. Whenever I rename a file,move a file, or take a photo from the android camera, nothing happens. Am I missing anything ? Am I monitoring wrong file?

Here is the whole FileObserver class

private static int mask = FileObserver.ALL_EVENTS; 

public PicturesFileObserver(String path,UploadPhotosService myService) {
    super(path,mask);
    this.rootDir = path;
    this.myService = myService;
    Log.e(TAG,"Observer is initialized on: "+rootDir);

}

@Override
public void onEvent(int event, @Nullable String path) {
   event &= FileObserver.ALL_EVENTS;
   Log.e(TAG,String.valueOf(event)); // ONLY PRINTS 512 
    if (path != null) {
        if (path.endsWith(".jpg") || path.endsWith(".png")) {
            if ((event == FileObserver.CREATE)) {
                Log.e(TAG, "File was created " + path);

            } else if ((FileObserver.MODIFY & event) != 0) {
                Log.e(TAG, "File was modified " + path);
            } else if ((FileObserver.DELETE & event) != 0) {
                Log.e(TAG, "File was deleted " + path);
            } else if ((FileObserver.MOVED_FROM & event) != 0) {
                Log.e(TAG, "File was brought here " + path);
            } else if ((FileObserver.MOVED_TO & event) != 0) {
                Log.e(TAG, "File was brought here " + path);
            }
        } else
            Log.e(TAG, "Path not beeing a photo: " + path);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
t_k_94
  • 37
  • 1
  • 4
  • without ```event &= FileObserver.ALL_EVENTS;```, what do you get? – user1506104 May 28 '18 at 13:03
  • What I found out is that when a photo is taken the System does not update the FileObserver instantly but after some time regardless the event &= FileObserver.ALL_EVENTS;. When I took a photo these are the codes I receive the following: 32 - OPEN 1 - ACCESS 16 - CLOSE NO WRITE in random order and random occurences of each event – t_k_94 May 28 '18 at 13:12
  • I still get nothing when I rename a File inside the folder. – t_k_94 May 28 '18 at 13:18
  • what is your minSdkVersion? – Sagar May 28 '18 at 14:33
  • people have reported an Issue in the past that FileObserver doesn't work on 6+ – Darshan May 28 '18 at 14:41
  • the minSdkVersion I have on my Project right now is 14 So, based on your comments, should Iquit the idea of using FileObserver or should I return to older versions?Any other alternative ? – t_k_94 May 29 '18 at 17:25

0 Answers0