SAF / MFile Tests

SAF / Mfile Tests

The following directories are being considered
/Documents, /Download, /Music , /Pictures, /Recordings, /Movies

I've written a test bed see MfileTest.aia. Using SAF I can read a text file from any of these directories, but using the same SAF pathname save to none. Using Mfile I can only save to Documents and Downloads, but using the same MFile pathname cannot read from them.

I’m struggling to understand what seems to be an asymmetry for these read/save actions. Is it Android related? Clearly there must be something wrong with my code. Maybe permissions, but having looked through some previous threads haven’t come up with a solution. I'd just love a decsion tree to determine what routines and permissions are required in particular circumstances - but it seems Google changes the rules every few months and something that worked 12 or 18 months ago now fails :frowning:

I think the Mfile ReadFrom would work with ApiLevel 33, but can’t see my error. In the case of MFile SaveFile (Anke?) did you decide to limit your extension to just /Documents and /Downloads? I’d really like to be able to write text files to Music as there is a format called abcNotation which is essentially text, although the file extension is .abc It been around for over 20 years. I though Android might have considered /Music to be a Shared folder?
Tests gave the following:
File Read Requests
SAF (Using OpenSingleDocument) can read a text file from all the above directories
The stem pathname being used is content://com.android.externalstorage.doduments.//tree/primary%3A/document/primary%3A Followed by DirName/Filename eg Documents/TestFile.txt

MFile (Using ReadFrom) fails to read from any of the above directories
The stem pathname being used is file:///storage/emulated/0/ followed by DirName/Filename eg Documents/TestFile.txt.

File Save Requests
SAF (Using WriteToFile) fails to save text file to any of the above directories
Get a permissions denied message.. requires MANAGE_DOCUMENTS or grantUriPermission(). Ive no idea how to do either of them.
The SAF stem pathname being used is the same as above for reading files

Mfile (Using SaveFile) can save to Documents, Download
But not to Music, Pictures, Recordings or Movies
The Mfile stem pathname being used is the same as above for reading files

MFileTest.aia (59.0 KB)

  1. Why not store your files in the ASD where you have read/write permission?
  2. You do not say what device and Android version you are testing on ?

these ares the correct directories for text files, the other directories are for media files only

Taifun

"I think the Mfile ReadFrom would work with ApiLevel 33"
:question:

"but can’t see my error."
Which error?

Since Android 11 "Scoped Storage" you can save non-media files only in the ASD, PrivateDir or in one of these Shared folders /Documents and /Download (without a "s" at the end).

See also here:

To read from file:///storage/emulated/0/Documents/TestFile.txt should work, if the non-media file (TestFile.txt) was saved with the same app. However, if you tested first with Companion and then with the APK it wont work, because the text file is then created by another app (Companion) and vice versa. You must first delete the file with Companion and then it will work also with the AKP and vice versa.

First you need to Open Document Tree and let user select a directory then grant your app access to it.
Only after getting access, you can create a document/file in that directory.

The abcNotation text files are either downloaded from The Session website or created myself in a text editor. The same file might be used in three different apps which rules out ASD

I suppose it depends on one's concept of media. Put an mp3 file into a suitable player app and it plays music. Put an abcNotation file into a suitable player app and it too will play music and could optionally display the musical score and lyrics. Googles concept of media is somewhat narrow, but I guess we have to live with it until they are more enlightened...

Sorry just noticed the multi-reply message. Will try an remember.
Thanks for your explanation Anke. I'm running apilevel 30 on the tablet and 33 on the phone. If I understand you correctly I'm maybe in a catch 22 situation. The abcNotation text files are from an external source, so will be seen as 'alien' by my app. Sometimes I wish to make a small edit - by loading the file into a textbox and then save it to the same filename. So far I've not got this to work - but that may be a 'media' issue as I've been trying to save to /Music. (Well abcNotation is actually music :slight_smile: )

Read this How to access non-media & media files on Android 11+ (again) and you will find everything you need to know.