My use-case is pretty simple:
- List files under a selected folder.
- Listen for any changes over this folder (deletion, renaming, creation, etc.). So, for instance, even if the user deletes a child file
A
using a third-party app (e.g the OS file manager) my app will be able to catch the event and delete the file reference, to avoid ghost files in my recycler view.
To list the files under a selected folder, which is a simple tree URI that my app gains access through openDocumentTree
, I'm already using SAF, I can use the tree URI to delete, rename, move child documents, etc. so permission is not a problem here.
But I am not able to find any resource pointing to some way to do the "listening" part. I've already:
- Read the Google storage documentation, but they cover every edge-case but not this one https://developer.android.com/guide/topics/data.
- I tried to see an example of it on the android/storage-samples repository but even there they are just reloading the entire directory when the user renames a folder, and it is not handling when the user does the alter by a third-party app, android/storage-samples/blob/22784d8cbf1d990958ae554ec61afea1a9da93c1.
- https://github.com/K1rakishou/Fuck-Storage-Access-Framework library, no mention to any kind of listenable APIs.
- https://github.com/anggrayudi/SimpleStorage library, no mention to any kind of listenable APIs.
- https://github.com/google/modernstorage, same...
- I even asked ChatGPT but he brings me a solution that doesn't works, the
ContentResolver.registerContentObserver
, which is exactly the approach of some unanswered SO questions (see below).
I found and tried some SO questions about it (a bit 'older' ~ 3 years), which also doesn't have answers yet:
My question is, do we have access to something that allow us to listen for changes over a folder that we've permission over?
I'm just about to give-up this shit and add MANAGE_EXTERNAL_STORAGE
permission and use the FileObserver API, and if it does not work either I think I'll just start brute-forcing the user storage searching for changes foff .