File saving and loading not working on Android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GodotEricGagnon

I am unable to save or load file when deploying my app on Android.

I am using the File class and the user:// directory.

I do not see any need for special Android permission for this use case.

Message when running the app on Android indicate a “file does not exist”, but not much more.

In Android Log:
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@309535f

Anyone having the same problem ?

Thanks !

Generally speaking, that should work fine. Does the app save correctly on a non-Android system? Perhaps you can share your save code?

jgodfrey | 2023-01-21 21:30

I just find out that the problem is not directly related to the saving/loading operations.

It has to do with the fact that I was trying to save a file in the _exit_tree() function. It works on Mac and Windows, but not on Android.

func _exit_tree():
    saveGame()

Here is my solution :

func _notification(what):

	if (what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST
		or what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST
		or what == MainLoop.NOTIFICATION_APP_PAUSED
		or what == MainLoop.NOTIFICATION_WM_UNFOCUS_REQUEST
		or what == MainLoop.NOTIFICATION_WM_FOCUS_OUT):

			saveGame()
			get_tree().quit()

func _exit_tree():
	get_tree().notification(MainLoop.NOTIFICATION_WM_QUIT_REQUEST)

GodotEricGagnon | 2023-01-22 15:21

Hello, Does this method apply to all types of Android?

terajane | 2023-03-15 05:37