3

Can someone possibly help me with this?

I want to observe a file to see if it gets modified so that I can update the activity. After several tests, I've determined it's just plain not working. Am I doing something wrong?

I'm creating a FileObserver with an onEvent method to display a Toast and log data just to see if it's working, however the onEvent is never getting called. I have tried it both with an existing and a new file, but it doesn't seem to work in either case.

    Context context = this;
    File fileFolder = context.getFilesDir();

    String fileName = "quest";
    FileObserver questObserver = new FileObserver(fileFolder.getPath()) { // also tried fileFolder.getName()
        @Override
        public void onEvent(int event, String path) {
            Toast.makeText(getApplicationContext(), "onEvent fired", Toast.LENGTH_LONG).show();
            Log.d(TAG, "FileObserver().onEvent");
        }
    };
    questObserver.startWatching();

    /* create file */
    ObjectOutputStream objectOut = null;
    try {
        FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        objectOut = new ObjectOutputStream(fileOut);
        objectOut.writeObject(new Quest());
        fileOut.getFD().sync();
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    } finally {
        if (objectOut != null) {
            try {
                objectOut.close();
            } catch (IOException e) {
                Log.d(TAG, e.getMessage());
            }
        }
    }

    /* read file */
    ObjectInputStream objectIn = null;
    Quest quest = null;

     try {
         FileInputStream fileIn = context.openFileInput(fileName);
         objectIn = new ObjectInputStream(fileIn);
         quest = (Quest) objectIn.readObject();
     } catch (FileNotFoundException e) {
         // Do nothing
     } catch (IOException e) {
         e.printStackTrace();
     } catch (ClassNotFoundException e) {
         e.printStackTrace();
     } finally {
         if (objectIn != null) {
             try {
                 objectIn.close();
             } catch (IOException e) {
                 Log.d(TAG, e.getMessage());
             }
         }
     }
     Toast.makeText(context, quest.getTitle(), Toast.LENGTH_LONG).show();

    questObserver.stopWatching();

Any help would be greatly appreciated.

Richard Rhyan
  • 203
  • 1
  • 6
  • 17
  • 3
    You need a complete path, not just the name of the file; "quest" - or is this something you just edited in when you pasted your code sample? Also, the file or folder needs to exist when you start observing it. – Jens Nov 25 '12 at 00:07

2 Answers2

2

'public abstract void onEvent (int event, String path)" -

This method is invoked on a special FileObserver thread. It runs independently of any threads, so take care to use appropriate synchronization! Consider using post(Runnable) to shift event handling work to the main thread to avoid concurrency problems.

http://developer.android.com/reference/android/os/FileObserver.html

If you put the toast through a handler.post(new Runnable(){...}), that should work.

rwx
  • 573
  • 1
  • 7
  • 22
1

Assuming your file doesn't (always) exist you should probably put your observer on the files folder, obtained like so:

Context ctx = ...;
File filesFolder = ctx.getFilesDir();

Note that this will also ensure that the filesFolder directory will be created.

Your observer will now be notified whenever a file is written, deleted or updated using for instance Context#.openFileOutput(..) - and you can filter in your FileObserver for the file name, in your example "quest".

Jens
  • 16,853
  • 4
  • 55
  • 52
  • I've edited my code, above.... it is still not firing the onEvent method. I'm able to verify that I'm writing and reading to the file. I'm thinking i'm not getting the path correctly from the filesFolder object. But I think it's right. (I have done very little with files at this point) – Richard Rhyan Nov 25 '12 at 15:40
  • 1
    That isn't the `FileObserver` malfunctioning - you are attempting to create a `Toast` inside the `onEvent` I noticed - that doesn't work since the `FileObserver` is running in a separate thread (and `Toast`s should be created on the UI-thread). If you need the `Toast`s you should make a `Handler` and post any info you want to show to that. – Jens Nov 25 '12 at 16:09
  • The Toast was just so I could see if it's working. I removed it and tried it again, and the log function still does not take effect. – Richard Rhyan Nov 25 '12 at 16:21
  • Nevermind... I tried it using fileFolder.getName(). But it works with fileFolder.getPath(). Thank you SO much for your help!! – Richard Rhyan Nov 25 '12 at 16:26