Home » Android » java – FileObserver does not work on external storage in Android 6.0 Marshmallow (API 23)

java – FileObserver does not work on external storage in Android 6.0 Marshmallow (API 23)

Posted by: admin April 23, 2020 Leave a comment

Questions:

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).
How to&Answers:

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.