2

I am trying to determine when a file is modified. Currently I have a class file observer class that contains

public class fileObserver extends FileObserver {
    public String absolutePath;
    public fileObserver(String path) {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
    }

    @Override
    public void onEvent(int event, String path) {
        if (path == null) {
            return;
        }
        if (event == FileObserver.MODIFY) {
            Log.d("change","changed");
        }
    }
}

and in my main activity I have

fileObserver test = new fileObserver(fullpathnamehere);
test.startWatching();

fullpathnamehere is the file path of the file that I am trying to observe. I checked and the file path is valid. Can anyone tell me what I'm doing wrong?

Alexander Wen
  • 33
  • 1
  • 7

4 Answers4

1

the method onEvent() is not being called because the path is incorrect:

event fileObserver test = new fileObserver(fullpathnamenere);
test.startWatching();

check the path of your file is correct!

Try using:

String fullpathnamenere = Environment.getExternalStorageDirectory().getPath() + "/stuff.txt";

UPDATE:

I have found that (thanks to Mr. Mark Murphy) the FileObserver is not recursive!

check this https://code.google.com/p/android/issues/detail?id=33659

I suggest the use of RecursiveFileObserver https://github.com/owncloud/android/blob/master/src/com/owncloud/android/utils/RecursiveFileObserver.java

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

Change your onEvent() method like this...

 @Override
    public void onEvent(int event, String path) {
        if (path == null) {
            return;
        }
        if ((FileObserver.MODIFY & event)!=0&&path.equals("file_name")) {
            Log.d("change","changed");
        }
    }

In Activity

fileObserver test = new fileObserver(parent_dir_path_of_the_file);
test.startWatching();
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
  • Register the file observer for the parent directory of the file and you will get the file name as the "path" string in the onEvent() method...this may works.... – Chandrakanth May 28 '15 at 05:10
  • Hi...@AlexanderWen have achieved the file observer functionality...? – Chandrakanth May 29 '15 at 10:38
  • No, I actually used a different approach to get the same result. Instead of observing when the data changed and then output the new data, I decided to just read the data at fixed intervals instead. – Alexander Wen May 29 '15 at 14:03
  • I think the problem is with the activity. You are calling startWatching() in the activity. Here what happens is when you come back from your activity the activity will be destroyed and file observer will be stopped. What you have to do is start a service and in the service initialize the file observer and start watching there. It may works for you...once try it and let me know is it working are not...I think reading the data at fixed intervals is a not a good idea!!! – Chandrakanth May 30 '15 at 07:15
0

Things you have to check:

  1. Check file path.
  2. Check file permission in manifest file.
  3. If its above 6.0 have to add permission dynamically.
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
user2851150
  • 397
  • 5
  • 12
0

Are you ready for the right answer?

I had the same problem, where I got no update from FileObserver. What could be the problem? File Observer is a very very very simple abstract class; with only three methods. And only one to implement!

I began doing test on different conditions and began to get a response from onEvent()! I copied a file using JStudio file manager and got a response. I then again used the File Manager I got with the phone. NO RESPONSE!!

So actually, you wont get events if your phone´s file management system does any changes. If a third party software does changes you are likely to be notified.

This sucks.

A solution might be to make a native file observer in C with JNI; using the linux api.