FileObserver does not work on external storage in Android 6.0 Marshmallow (API 23)
Asked Answered
M

1

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).
Mv answered 2/9, 2015 at 13:25 Comment(15)
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.Shiverick
Make sure to keep a reference to the FileObserver somewhere. If it is garbage collected, it will stop receiving events.Sennit
Also, FWIW, there is an issue filed about this.Shiverick
@Shiverick Yes, by API version I meant targetSdkVersion. Also, thanks for the code.google.com link. It looks very much the same problem I face.Mv
@GaborFekete Have you solved the problem yet? I'm using API 22 and want to detect new screenshots. But FileObserver doesn't work.Trogon
@Sunshinetpu I gave up for now, hoping it'll get magically fixed. Meanwhile you can find my test app here: github.com/feher/Android-FileObserverTestMv
Hi @feher-melos, i met the same problem, nexus 5,Android 6.0.0 (MRA58K). According to the issue, this is still a new-issue is not solved yet.Vintager
Anyone have any news on whether this issues has a workaround yet?Deckhouse
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 nowDeckhouse
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.Shultz
@Deckhouse I have 0774, still no eventsApologete
@Apologete are you using LP or MM?Deckhouse
@Deckhouse 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.Apologete
@Apologete yes this fixed the issues for me on Lollipop, I suspect there is a deeper issue in MarshmallowDeckhouse
@Mv for more update you can check here issuetracker.google.com/issues/37065227Phelgon
A
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.

Agnostic answered 27/7, 2017 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.