Page 1 of 1

Problems at writing files in External Storage (Android)

Posted: Fri Nov 05, 2021 7:37 pm
by CharlieFuu69
As I was reading in the Ren'Py documentation, the renpy.request_permission() function was added in v7.4.9.
I wanted to put it into practice in my game so that the player could have saved a TXT file with their Nickname and account password. For this I decided to use the following line to request the storage permission:

Permission request in runtime :

Code: Select all

$ renpy.request_permission("android.permission.WRITE_EXTERNAL_STORAGE")
That line makes the runtime request for write permission. So far everything is fine.
The problem lies at the time of writing or creating a folder in the player's storage, which I use the following code:

Make directory :
Considering that the "os" module was imported

Code: Select all

def dir_startup():
    android_user_path = os.environ["EXTERNAL_STORAGE"] + "/Game Files"
    
    if os.path.exists(android_user_path):
        pass ## If the folder exists, it is ignored.
        
    else:
        try:
            os.mkdir(android_user_path)
            print("> Success!")
        except Exception as mkerr:
            print("> Error :", mkerr) ## Catch the exception without crashing the game.
TXT file write :

Code: Select all

def backup_player_id():
    android_user_path = os.environ["EXTERNAL_STORAGE"] + "/Game Files"
    
    try:
        with open(android_user_path + "/Player ID.txt", "w") as f:
            f.write("...") ## Player data is written here.
            f.close()
        print("> Write success!")
    except Exception as writerr:
        print("> Error :", writerr)
When executing those lines of writing, the following exception appears:

Code: Select all

[Errno 1] Operation not permitted: u'/sdcard/Game Files/Player ID.txt'
With the "WRITE_EXTERNAL_STORAGE" permission I couldn't create a folder in /sdcard or /storage/emulated/0, but I could create it in /storage/emulated/0/DCIM. I thought that I could write files there, but when trying to write them within that path, the error above appears again.
I tried to use the "MANAGE_EXTERNAL_STORAGE" permission to give me more freedom to create those folders and write files, but apparently renpy.request_permission() ignores it and doesn't bring up the request at runtime.

This issue would ruin part of the in-game experience, as I plan to make it possible for players to download the tracks to their device if they wish.
Is there a way for Android to allow me to create folders and write files normally? Or this no longer has a solution?

NOTE : As additional information, I am running these tests on a mobile with Android 11.

Re: Problems at writing files in External Storage (Android)

Posted: Sun Nov 07, 2021 9:30 pm
by PyTom
Modern versions of Android basically prevent you from writing to external storage like that. To make it work, you'll need ask for access to specific folders. That will require a lot of programming with pyjnius, and I'll admit that I was never able to get that working normally.

It's probably best to treat it as a problem without a good solution. Android security is too rough.