miriskrit
miriskrit

Reputation: 77

downloading images from the file system to kivy app

I have a simple python kivy application and sqlite database.

I want to realize the ability to add my pictures from the phone to the application

How to open file manager when the user clicks the button, or any other way. The file should be uploaded to the folder where it is located my app (in ./images folder), and its name added to my database

is this possible? Or is it better to upload images to a database? (no plans to use more than 10 images)

I will be glad to see links to suitable solutions in the documentation, or examples

UPDATE

kivy FileChooser works work on my PC (ubuntu), but on Phone it open a root folder that you can't get anywhere from

Upvotes: 1

Views: 1021

Answers (1)

Ne1zvestnyj
Ne1zvestnyj

Reputation: 1397

It’s work for me

from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.modalview import ModalView
from plyer import storagepath

from kivymd.uix.filemanager import MDFileManager
from kivymd.theming import ThemeManager
from kivymd.toast import toast


Builder.load_string('''


<ExampleFileManager@BoxLayout>
    orientation: 'vertical'
    spacing: dp(5)

    MDToolbar:
        id: toolbar
        title: app.title
        left_action_items: [['menu', lambda x: None]]
        elevation: 10
        md_bg_color: app.theme_cls.primary_color


    FloatLayout:

        MDRoundFlatIconButton:
            text: "Open manager"
            icon: "folder"
            pos_hint: {'center_x': .5, 'center_y': .6}
            on_release: app.file_manager_open()
''')


class Example(MDApp):
    title = "File Manage"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Window.bind(on_keyboard=self.events)
        self.manager_open = False
        self.manager = None

    def build(self):
        return Factory.ExampleFileManager()

    def file_manager_open(self):
        if not self.manager:
            self.manager = ModalView(size_hint=(1, 1), auto_dismiss=False)
            self.file_manager = MDFileManager(
                exit_manager=self.exit_manager, select_path=self.select_path)
            self.manager.add_widget(self.file_manager)
            self.file_manager.show(storagepath .get_home_dir())  # output manager to the screen
        self.manager_open = True
        self.manager.open()

    def select_path(self, path):
        '''It will be called when you click on the file name
        or the catalog selection button.

        :type path: str;
        :param path: path to the selected directory or file;
        '''

        self.exit_manager()
        toast(path)

    def exit_manager(self, *args):
        '''Called when the user reaches the root of the directory tree.'''

        self.manager.dismiss()
        self.manager_open = False

    def events(self, instance, keyboard, keycode, text, modifiers):
        '''Called when buttons are pressed on the mobile device..'''

        if keyboard in (1001, 27):
            if self.manager_open:
                self.file_manager.back()
        return True


Example().run()

KivyMD file manager

plyer

Upvotes: 1

Related Questions