[Python] APK with storage issue

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to get image path from user device through file picker, store the path information in client.storage file for later use, and to use this image as an ElevatedButton. While this is working on Windows, it is not the case on Android.

    I built the APK with the following command: flet build apk --include-packages flet_permission_handler -vv

    Code sample:

    import flet as ft
    import os


    def get_folder(page, base_path):
    store = {}
    image_extensions = ['.jpg', '.jpeg', '.png']
    for item in os.listdir(base_path):
    item_path = os.path.join(base_path, item)
    if os.path.isfile(item_path) and any(item.lower().endswith(ext) for ext in image_extensions):
    tmp = page.client_storage.get("lib")
    page.client_storage.set("lib", page.client_storage.get("lib") + [item_path])
    print(page.client_storage.get("lib"))
    page.update()
    break

    def build_home(page):
    imagesPath = page.client_storage.get("lib")
    images = ft.ResponsiveRow()
    for item in imagesPath:
    images.controls.append(
    ft.ElevatedButton(
    col={"sm": 12, "md": 4, "xl": 2},
    content=ft.Image(
    src=item,
    border_radius=ft.border_radius.all(20)
    ),
    style=ft.ButtonStyle(
    shape={ft.MaterialState.DEFAULT: ft.RoundedRectangleBorder(radius=20)},
    padding={ft.MaterialState.HOVERED: 0}
    )
    )
    )

    page.update()
    return images

    def main(page: ft.Page):
    if page.client_storage.contains_key("lib"):
    print("storage exists")
    print(page.client_storage.get("lib"))
    else:
    page.client_storage.set("lib", [])

    # PermissionHandler ------------------------------------------------
    ph = ft.PermissionHandler()
    page.overlay.append(ph)

    def request_permission_media(e=None):
    try:
    result = ph.request_permission(ft.PermissionType.MEDIA_LIBRARY)
    except Exception as ex:
    print(f"Media Library Permission Error: {ex}")

    def request_permission_external_storage(e=None):
    try:
    result = ph.request_permission(ft.PermissionType.STORAGE)
    except Exception as ex:
    print(f"Storage Permission Error: {ex}")

    # DATA path--------------------------------------------
    page.storage = list(page.client_storage.get_keys("lib"))

    ## Home page--------------------------------------------
    if len(page.storage) > 0:
    home = build_home(page)
    home.visible = True
    else:
    home = ft.ResponsiveRow()

    ### Folder picker --------------------------------------
    def pick_files_result(result: ft.FilePickerResultEvent):
    if result.path:
    get_folder(page, result.path)
    page.resultPath = result.path
    page.storage = page.client_storage.get_keys("key-prefix.")
    page.controls[0] = build_home(page)
    page.update()

    page.pick_files_dialog = ft.FilePicker(on_result=pick_files_result)

    page.overlay.append(page.pick_files_dialog)

    # Build pages--------------------------------------------
    #page.add(page.home)#, page.chapters)
    page.floating_action_button = ft.FloatingActionButton(
    icon=ft.icons.ADD,
    tooltip="Add a story",
    shape=ft.CircleBorder(),
    width=100,
    mini=True,
    on_click = lambda _: page.pick_files_dialog.get_directory_path()
    )


    page.add(home) # page.controls[0]
    page.update()

    ft.app(target=main, upload_dir="assets")


    If I run this on Windows and pick a folder where an image ['.jpg', '.jpeg', '.png'] is present, I get the ElevatedButton created with the image within the folder. However, on Android nothing happens.

    Continue reading...

Compartilhe esta Página