Thanks! I Also had to include the file name in "dirpath":
so dirpath is now:
/storage/emulated/0/Android/data/appinventor.ai_ugmcp_somincor.ContactosSomincor/files/contactos.txt
The error message is:
'Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{95a49dd 24119:appinventor.ai_ugmcp_somincor.ContactosSomincor/u0a205} (pid=24119, uid=10205) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs'.
So on first run of the app, get the user to go off and pick the file, TakePersistableUriPermission for the contenturi, then store the contenturi for subsequent use. Have a check in place if the automatic method fails, so that the user can go off and pick the file again.
As said above not sure what happens when the original file is updated/replaced, you will need to test this (and please report back)
If the user has done this once at the beginning, he already has full access to the file and can of course, as I have shown, copy it to one of the Shared folders or the ASD. After that, the app has full access to this file without any permissions, since it created this file itself.
These statements (regarding the Shared folder) of course only apply to Android 11+.
We have perhaps not yet asked the right question. Where does this text file come from, and how does it get onto a users device. Is it created/updated by another app?
First of all thank you very much for taking the time to help me.
So, I have developed an App that updates the phone's contact list based on the content of a text file.
We used Airwatch (MDM software) to transfer the text file to the ASD, but after Android 10 this is not possilble anymore. Airwatch can only access Shared folders, like Documents or Download.
So my idea was to trasnfer the text file to one of these folder and let the App copy it to the ASD or read it directly from the Shared Folder.
But apparently for this to be possible with SAF there must some user interaction, to let the user select the file, at leats for the first time.