Android Question Read/write File.DirDefaultExternal - EACCES (permission denied)

DIYRicey

Member
Licensed User
Longtime User
There are a lot of threads that seem to relate to my query, and I have tried to study them, but cannot find an answer to this specific case. I want to be able to send users a specialised data file by email or whatever, and for them to be able to open the file using my app. I hope to distribute the app (for free) using Google Play once I have sorted out this problem. The data files relate to tower bell-ringing.
I have achieved this for Android 5. Using the app I can read/write custom data files from/to folders in File.DirDefaultExternal. I can use a File Manager to check everything and it all makes sense.
For my Moto G9 running Android 11, when I try to read the folder I can see/navigate folders, but cannot see any of the files, although a File Manager confirms the files are there, with the right extensions. If I write a file using the app I can then see it (and open it) using the app and I can also see it using a File Manager. The same goes for my Pixel 6A running on Android 16.

If I try to write a file with the same name as one already in the relevant folder, but invisible to my app's read function, first of all I get a warning about overwriting the file (code in my app does this) but then, if I try to go ahead and overwrite I get the EACCES message.


I have added
B4X:
SetApplicationAttribute(android:requestLegacyExternalStorage, "true")
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
to the Manifest.

Using the List Permissions facility in B4A I get
permissions.png


Do I need to use RuntimePermissions? If so how?

Many thanks.
 

drgottjr

Expert
Licensed User
Longtime User
and my 2 cents:
"by email or whatever". hard to know where to begin.

the image relating to permissions that you show refers to "dangerous permission". that's why your app doesn't work with devices running newer than android 5.

how the file reaches your users is crucial. if it's by email, then their email client will presumably offer to save it. that will probably be in the "files" or "download" system folder. that means you have to provide a contentchooser in order for them to access it within your app. no special permission required for that. since you mention the file is a "specialised data file", then it will probably not have a recognized mime type (required for contentchooser). that means contentchooser will display all the files in the given folder, and the user will have to select it. not a big problem unless the user has thousands of files in that folder...

if the file reaches your users by "whatever", that could be a longer story.

if you're looking for somewhere to save files (for yourself or for your uses), especially for users with devices newer than android 5, there are some options. if you kept the files in the cloud, your app could download them and the users save the ones they wanted to keep in locations which are under the control of your app (DirInternal or GetSafeDirDefaultExternal("")). there is also the SaveAs routine which kind of acts like DirDefaultExternal did, back in the day.

you need to decide how users gain access to the files, taking into account that some users may have updated their devices since android 5
 
Upvote 0

DIYRicey

Member
Licensed User
Longtime User
Thanks for the advice. Thanks in particular for pointing out that 'dangerous' permissions are an immediate fail point for Android > 5, so trying ta access DirDefaultExternal is going to be a fruitless aim. I will have a play with the B4X Text Editor example. At least it finds/shows my files OK. Seem to be a lot of new concepts for my poor old brain to cope with.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Seem to be a lot of new concepts for my poor old brain to cope with.
Read:

- READ_EXTERNAL_STORAGE / WRITE_EXTERNAL_STORAGE

- 29 - No permission to access File.DirRootExternal, even with the STORAGE permission.

- RuntimePermissions.GetSafeDirDefaultExternal - A folder on the secondary storage, where you app can access without permissions. The path is a bit cumbersome (Log it to see). Note that other apps can't access this folder.
 
Upvote 0
Top