Skip to content

my way to get android files real path when flet run --android. #203

@leefm0204

Description

@leefm0204

flet version=0.28.2
devices= android phone, proot-ubuntu
use flet for android app as display after run flet run --android.

issue

  1. flet filepicker only get cache path for selected file instead real path

how to get real path: think out of box:

Strategy
1.Extract` filename from FilePickerResultEvent.

  1. obtain directory path from get_directory_path().

  2. Reconstruct real path using:
    `os.path.join(real_dir_path, extracted_filename)

below is example of my file_picker code to get real path of selected file:

def pick_files_result(e: ft.FilePickerResultEvent, selected_files: ft.TextField):
    if e.files:
        real_folder = selected_folder_path.value  # use the folder picked via get_directory_path
        selected_paths = []
        for f in e.files:
            filename = os.path.basename(f.name or f.path)  # get file name
            if real_folder:
                full_real_path = os.path.join(real_folder, filename)
                selected_paths.append(full_real_path)
            else:
                selected_paths.append(f.path or f.name)
        selected_files.value = ", ".join(selected_paths)
        selected_files.update()

selected_folder_path = ft.TextField(visible=False)

def get_directory_result(e: ft.FilePickerResultEvent, directory_path: ft.TextField):
    if e.path:
        directory_path.value = e.path
        selected_folder_path.value = e.path  # store for use in pick_files_result
        directory_path.update()
def folder_picker_dialog(path_field: ft.TextField):
    pick_folder_dialog = ft.FilePicker(
        on_result=lambda e: get_directory_result(e, path_field)
    )
    pick_folder_trigger = ft.ElevatedButton(
        "Open directory",
        icon=ft.Icons.FOLDER_OPEN,
        on_click=lambda _: pick_folder_dialog.get_directory_path(),
    )
    return pick_folder_trigger, pick_folder_dialog

def file_picker_dialog(path_field: ft.TextField):
    pick_files_dialog = ft.FilePicker(
        on_result=lambda e: pick_files_result(e, path_field)
    )
    pick_files_trigger = ft.ElevatedButton(
        "Pick files",
        icon=ft.Icons.FOLDER_OPEN,
        on_click=lambda _: pick_files_dialog.pick_files(
            allow_multiple=True, allowed_extensions=["srt", "ass"],
        ),
    )
     pick_files_trigger, pick_files_dialog
def main(page: ft.Page):
    page.theme_mode = "dark"
    pick_files_trigger, pick_files_dialog = file_picker_dialog(path_field)
    pick_folder_trigger, pick_folder_dialog = folder_picker_dialog(path_field)

    page.overlay.extend([folder_picker, file_picker])
    page.add(
        ft.Column([
            ft.Row([folder_path_field, pick_folder_btn]),
            ft.Row([file_path_field, pick_files_btn]),
            selected_folder_path  # hidden storage
        ])
    )
ft.app(target=main)


remark:

  1. in app, we need to click 'Open Directory' first to grant access to the directory path

  2. and then click 'pick file' to select file in the directory we grant access to get real path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions