Problems at writing files in External Storage (Android)

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
CharlieFuu69
Regular
Posts: 30
Joined: Mon Nov 11, 2019 10:32 pm
Projects: Current projects using Ren'Py : "Tears: The First Love!" (Visual Novel at 90%) , "ElectroBasics Electronic Quiz!" (Educative game at 45%).
Location: Chile
Contact:

Problems at writing files in External Storage (Android)

#1 Post by CharlieFuu69 » Fri Nov 05, 2021 7:37 pm

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.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

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

#2 Post by PyTom » Sun Nov 07, 2021 9:30 pm

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.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: span4ev