android.os.FileObserver
requires a java.io.File
to function.
But with Android 10 Google restricted access to everything but your app's private directory due to the famous "Storage Access Framework". Thus, accessing anything via java.io.File
breaks and renders FileObserver
useless unless you intend to use it in your app's private directory. However, I want to be notified when something is changed in a certain directory on external storage. I would also like to avoid periodically checking for changes.
I tried using ContentResolver.registerContentObserver(uri,notifyForDescendants,observer)
and ran into some problems with that method:
- Every
Uri
I have plugged in so far was accepted - It neither fails nor notifies if the
Uri
doesn't work - I cannot find any documentation telling me which
Uri
s actually work
The only thing I got working to some extent is the following approach:
// works, but returns all changes to the external storage
contentResolver.registerContentObserver(MediaStore.Files.getContentUri("external"), true, contentObserver)
Unfortunately this includes all of the external storage and only returns Media Uri
s when changes happen - for example content://media/external/file/67226
.
Is there a way to find out whether or not that Uri
points to my directory?
Or is there a way to make registerContentObserver()
work with a Uri
in such a way that I get a notification whenever something in the folder has changed?
I also had no success trying various Uris related to DocumentsFile
and external storage Uri
s.