32

I have an app that observes a public directory on external storage with FileObserver. It works fine on Lollipop devices. I want to add support for Marshmallow, so I set up a Nexus 9 tablet with it.

On the Marshmallow device, it fails, on Lollipop device it's OK. On Marshmallow device, the FileObserver does not react to file system events that are caused by other processes. E.g. taking a screenshot, creating files via adb shell. It works fine if the files are created by my app.

On Marshmallow, I ask for WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions from Manifest.xml.

When I compile with API 23, I also make sure that I call ActivityCompat.requestPermissions().

When I compile with API 22, I just rely on Manifest.xml.

It works if I observe stuff on /data/data/my-package-name.

It fails if I observe stuff on /storage/emulated/0/Pictures/Screenshots.

Did anybody test FileObserver considreing all of the following? :

  • Marshmallow device
  • API 23 and API 22?
  • external storage
  • files created by some other process than the observing app (e.g. adb or taking screenshot).
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
feher-melos
  • 321
  • 3
  • 4
  • Have you tried other locations on external storage? It is possible that your problem is limited to that directory, given its somewhat sensitive nature. BTW, what you compile with has no bearing on the behavior of your app with regards to runtime permissions; your `targetSdkVersion` does. – CommonsWare Sep 02 '15 at 14:07
  • Make sure to keep a reference to the FileObserver somewhere. If it is garbage collected, it will stop receiving events. – Mister Smith Sep 02 '15 at 14:12
  • Also, FWIW, [there is an issue filed about this](https://code.google.com/p/android-developer-preview/issues/detail?id=3099). – CommonsWare Sep 02 '15 at 21:27
  • @CommonsWare Yes, by API version I meant targetSdkVersion. Also, thanks for the code.google.com link. It looks very much the same problem I face. – feher-melos Sep 03 '15 at 04:38
  • @GaborFekete Have you solved the problem yet? I'm using API 22 and want to detect new screenshots. But FileObserver doesn't work. – TOP Sep 05 '15 at 04:37
  • 1
    @Sunshinetpu I gave up for now, hoping it'll get magically fixed. Meanwhile you can find my test app here: https://github.com/feher/Android-FileObserverTest – feher-melos Sep 07 '15 at 05:37
  • Hi @feher-melos, i met the same problem, nexus 5,Android 6.0.0 (MRA58K). According to the [issue](https://code.google.com/p/android/issues/detail?id=189231), this is still a new-issue is not solved yet. – kvh Oct 23 '15 at 08:52
  • Anyone have any news on whether this issues has a workaround yet? – rosterloh Apr 19 '16 at 11:37
  • I'm not sure if this is helpful to anyone but with API 22 I found that the directory I was watching was 0700, I have changed it to 0770 and I am getting events from FileObserver now – rosterloh Apr 20 '16 at 08:42
  • Strangely when I upgraded my Samsung Galaxy S6 to Marshmallow I stopped receiving FileObserver.CREATE or FileObserver.CLOSE_WRITE on new files created, but I am still getting FileObserver.OPEN / ACCESS and CLOSE_NOWRITE events. – MikeL Jun 10 '16 at 14:34
  • @rosterloh I have 0774, still no events – user149408 Jun 13 '16 at 20:54
  • @user149408 are you using LP or MM? – rosterloh Jun 15 '16 at 07:49
  • @rosterloh Marshmallow. Re-reading your comment, you seem to describe a permissions problem on a Lollipop (or earlier) system that doesn't have the bug. Since Android creates different user IDs for each app, mode 0700 would not grant any access to any other app, which would presumably also prevent a `FileObserver` from firing. – user149408 Jun 15 '16 at 09:17
  • @user149408 yes this fixed the issues for me on Lollipop, I suspect there is a deeper issue in Marshmallow – rosterloh Jun 16 '16 at 09:41
  • @feher-melos for more update you can check here https://issuetracker.google.com/issues/37065227 – Prags Dec 16 '17 at 04:27

1 Answers1

2

This appears to be a bug in Marshmallow, see this link.

This is not fixed even in Nougat, you could get rid of the API this whole FileObserver, as it makes completely useless the fact that on most of the devices, it will not work.

Divy Soni
  • 824
  • 9
  • 22