FileObserver not recursive?

953 views
Skip to first unread message

jrm

unread,
Jul 22, 2010, 10:47:21 AM7/22/10
to android-platform
Hi,

According to Android Developers Reference, FileObserver should be
recursive.
But on my side, it is not:
My observer is monitoring all events on a directory called DIRECTORY1.
- If I add a file inside DIRECTORY1, my observer receives an event.
- If I add a new directory DIRECTORY2 inside DIRECTORY1, my observer
receives an event.
- But if I add a file inside DIRECTORY2, my observer does not receive
any event ... is it normal?

Thanks in advance

Dianne Hackborn

unread,
Jul 22, 2010, 12:33:42 PM7/22/10
to android-...@googlegroups.com
If the docs says it is recursive, the doc is wrong.  The underlying Linux facility for this is not recursive.


--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

jrm

unread,
Jul 23, 2010, 11:44:26 AM7/23/10
to android-platform
Ok, but how to implement such mechanism that monitors files and
directories recursively ?
Because FileObserver event callback does not allow to retrieve the
full path of the monitored file ...

Thanks

On Jul 22, 6:33 pm, Dianne Hackborn <hack...@android.com> wrote:
> If the docs says it is recursive, the doc is wrong. The underlying Linux
> facility for this is not recursive.
>
>
>
> On Thu, Jul 22, 2010 at 7:47 AM, jrm <jrmphl...@gmail.com> wrote:
> > Hi,
>
> > According to Android Developers Reference, FileObserver should be
> > recursive.
> > But on my side, it is not:
> > My observer is monitoring all events on a directory called DIRECTORY1.
> > - If I add a file inside DIRECTORY1, my observer receives an event.
> > - If I add a new directory DIRECTORY2 inside DIRECTORY1, my observer
> > receives an event.
> > - But if I add a file inside DIRECTORY2, my observer does not receive
> > any event ... is it normal?
>
> > Thanks in advance
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-platform" group.
> > To post to this group, send email to android-...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-platfo...@googlegroups.com<android-platform%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-platform?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Jul 23, 2010, 12:52:57 PM7/23/10
to android-...@googlegroups.com
Iterate over the directory recursively, observing each directory recursively; add and remove directories as your observers tell you about them changing.

To unsubscribe from this group, send email to android-platfo...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

jrm

unread,
Jul 26, 2010, 5:17:12 AM7/26/10
to android-platform
Ok but as I told you, the event handler of FileObserver does not
provide the full path of the file or directory that generates this
event.
I mean, if you have a single event handler to monitor several level of
directories, you cannot know which level of directory generates the
event because the FileObervser handler does not provide it ...
> > <android-platform%2Bunsu...@googlegroups.com<android-platform%252Buns...@googlegroups.com>

John Vidler

unread,
Jul 26, 2010, 11:05:55 AM7/26/10
to android-...@googlegroups.com
As Dianne said (correct me if I'm off here!), if you create a tree
structure of directory objects each observing their child files,
then when any given file changes it will notify it's folder parent
which can in turn notify its parent until you get to the top layer.

That way, you can build the path as the notifications go through the
layers, although I shudder to think of the memory usage if you have
a deep hierarchy.

I'll hack together something in a bit and report on the results.

To unsubscribe from this group, send email to android-platfo...@googlegroups.com.

John Vidler

unread,
Jul 26, 2010, 12:20:25 PM7/26/10
to android-...@googlegroups.com
Seems to work nicely, the strings reported here are all being handed up to the activity:

'Only' used ~1MB to monitor my entire SD card directory, I'll let you be the judge on how
good that is.
Reply all
Reply to author
Forward
0 new messages