0 votes

Trying to write out data to a file, preferred to be in the Documents folder. However, I cannot figure out how to gain access to it. Using OS.getsystemdir(2) I get:
User directory: /storage/emulated/0/Download/ testfolder doesn't exist. /storage/emulated/0/Download/testfolder/testfile.txt opened. fpuser error code: 12 /storage/emulated/0/Download/testfolder/testfile.txt written.
Yet there is clearly nothing written to the Download folder or the Documents folder. According to the documentation I should be using 2 as the reference to Documents, but it doesn't return Documents, it returns Downloads. And 3 also returns Downloads. https://godot.readthedocs.io/en/stable/classes/class_os.html

asked Sep 12 in Engine by Chickenscratch (20 points)

Note that you can write SYSTEM_DIR_DOCUMENTS instdead of 2, because the number can change over versions, while the named constant will stay the same.
Also, which Godot version are you using?

2.1.3 and I've tried 2.1.4rc. Android 6.0.1 if it matters.
I just tried user_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
& user_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS).
Same results.

if( !dir.dir_exists( dir_name ) ):
  print( dir_name + " doesn't exist." )
  dir.make_dir_recursive( dir_name )
else:
  print( dir_name + " exists." )

WITH the external storage permission checked.

User directory: /storage/emulated/0/Download/ testdir doesn't exist.
/storage/emulated/0/Download/test
file.txt opened. fpuser error code:
12 /storage/emulated/0/Download/test
file.txt written.

Write (not read) external storage is enabled?

Maybe there is another custom permission needed on that android version...
There is a read/write_media_storage and read/write_external_storage, I don t know the differences (r/w media storage is not on the exporter list of options).

Yup, this is all the permissions I've had selected. I don't see any other media or storage related options.
https://imgur.com/a/7t9fY

I have read about some other permissions but can't find anything on the reference.
Maybe you can try with logcat to see if something else is happening.

So it turns out that it is a 'problem' with Android 6 and beyond that I didn't know about/understand as I am not an Android developer. In 6+ the app has to be manually granted storage permissions despite having it granted via the export settings! How annoying. Apparently this isn't a problem if you're deploying your app to the Play store and it gets downloaded and installed that way. However, side-loading or one-click deploy from Godot engine will fail. In my case, the app will never be added to the Play store as this is a work project and will only be side loaded to our tablets for internal use.

In the end I'm using this for Android environments as my directory to write to;
user_dir = "/storage/emulated/0/Download/"

And still using this for any deployment to a Windows box;
userdir = OS.getsystemdir(OS.SYSTEMDIR_DOCUMENTS)

Also note that in Godot, the "Clear Previous Install" option must be unchecked (thanks volhs!) else you won't be able to properly debug in Godot because the write permissions will be reset to not allowed.

interesting find!

I guess that should be reported, maybe in the docs repository so it can be added on the documentation (I don't know if something could be done on the engine-side for debugging).

ps: you can answer yourself and mark that as best answer.

1 Answer

+1 vote

Much thanks to @vohlz for helping diagnose these issues over on Godot github....much of his words used here:

With Android 6 and beyond your app has to be manually granted storage permissions (via application settings on the device) despite having it granted via the export settings/manifest! This appears to be only true while you are developing and will not be an issue once the app is deployed to the Play store and it gets downloaded and installed that way. However, side-loading or one-click deploy/debug from Godot engine will fail. In my case, the app will never be added to the Play store as this is a work project and will only be side loaded to our tablets for internal use. Hence there needs to be another way.

So...the "Clear Previous Install" option must be unchecked in Godot or you won't be able to properly debug because the write permissions will be reset due to the manual grant thing. This was the major hangup for me as I was trying to debug and couldn't get any debug info back as each time I deployed with remote debug, it would reset the permissions due to uninstall and reinstall of the app, which meant I couldn't see what was going wrong.

In the end I'm manually using this for Android environments as my directory to write to;
user_dir = "/storage/emulated/0/Download/"

And still using this for any deployment to a Windows environment;
userdir = OS.getsystemdir(OS.SYSTEMDIR_DOCUMENTS)

answered 4 days ago by Chickenscratch (20 points)

glad to help :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.