How can I get permission to save file for flet app on android?

201 Views Asked by At

I'm building an app with python flet that generate some text content and allow user to choose target folder to save them (as a .txt file).

Here is the sample code for file saving I'm trying to use:

import os
import flet as ft

def main(page:ft.Page):
    
    # Function to Create Text File
    def on_dialog_result(e: ft.FilePickerResultEvent):
        trg = e.path
        try:
            with open(os.path.join(trg,"Test.txt"),"w+",encoding="utf8") as fl:
                fl.write("Boo!")
            fl.close()
            res = "Save to "+trg
        except Exception as e:
            res = str(e)
        pth.value = res
        page.update()
    
    # File Picker Object
    file_picker = ft.FilePicker(on_result=on_dialog_result)
    page.overlay.append(file_picker)

    # Message Display
    pth = ft.Text()
    
    # Add to Page
    page.add(
        ft.ElevatedButton(
            "Choose files...",
            on_click=lambda _: file_picker.get_directory_path()
        ),
        pth,
    )
    page.update()

ft.app(target=main)

But after I build apk (with flet build cli tool) and tested on the emulator, it throws the "Permission denied" error.

Error Message on Emulator

Is there any possible way to enable the file saving permission for my flet app?

What I've tried :

  1. The file access permission has enabled in the android system settings.
  2. File picker can view and get the folder path, but can not save anything to the path.
0

There are 0 best solutions below