Why do I get fp_user error 12 trying to write to user://

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chickenscratch
:warning: Old Version Published before Godot 3 was released.

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.get_system_dir(2) I get:
User directory: /storage/emulated/0/Download/ test_folder doesn’t exist. /storage/emulated/0/Download/test_folder/test_file.txt opened. fp_user error code: 12 /storage/emulated/0/Download/test_folder/test_file.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. OS — Godot Engine (stable) documentation in English

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?

Zylann | 2017-09-13 12:59

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/ test_dir doesn’t exist.
/storage/emulated/0/Download/test_file.txt opened. fp_user error code:
12 /storage/emulated/0/Download/test_file.txt written.

Chickenscratch | 2017-09-13 17:28

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).

eons | 2017-09-14 02:58

Yup, this is all the permissions I’ve had selected. I don’t see any other media or storage related options.
Imgur: The magic of the Internet

Chickenscratch | 2017-09-14 14:13

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.

eons | 2017-09-14 23:17

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;
user_dir = OS.get_system_dir(OS.SYSTEM_DIR_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.

Chickenscratch | 2017-09-15 16:34

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.

eons | 2017-09-16 14:19

:bust_in_silhouette: Reply From: Chickenscratch

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)

glad to help :slight_smile:

volzhs | 2017-09-18 16:46