python - 从库 Python Kivy for Android 中选择图像

标签 python android image kivy

当我单击 Python Kivy 的按钮时,我可以从图库中加载图像吗?我是初学者,在网上找不到例子。

我想做一个简单的应用程序,您需要从手机中选择一张图片并将其显示在屏幕上。

例如,要打开这样的菜单:

Example

我找到了设备上所有目录的文件选择器示例,但它在 android 上不起作用(当我尝试打开某个目录时,应用程序终止)

Python:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.image import Image

import os

class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    lbl_path = ObjectProperty(None)
    img = ObjectProperty(None)
    picture_path = ""

    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        with open(os.path.join(path, filename[0])) as img:
            self.img.source = ''.join(filename)
            self.picture_path = self.img.source
            self.lbl_path.text = self.picture_path

        self.dismiss_popup()


class Editor(App):
    pass


Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)

if __name__ == '__main__':
    Editor().run()

基维(editor.kv):
Root:
img: img
lbl_path: lbl_path

canvas.before:
    Color:
        rgba: 1, 1, 1, 1
    Rectangle:
        pos: self.pos
        size: self.size

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: None
        height: 30
        Button:
            text: 'Load'
            on_release: root.show_load()

    BoxLayout:
        padding: 20

        GridLayout:
            rows: 2
            Image:
                id: img
                size_hint: (.8, .8) 

            GridLayout:
                rows: 3
                Label:
                    id: lbl_path
                    color: (1, 0, 0, 1)
                    text: "Picture:"
                    size_hint: (.8, .8)  

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

        Button:
            text: "Load"
            on_release: root.load(filechooser.path, filechooser.selection)

最佳答案

如果您想访问 android 手机存储数据(图像、视频等),您需要为此授予权限
你可以分两步完成

  • 将您的权限添加到 android.permissions在你的规范文件中是这样的:android.permissions = READ_EXTERNAL_STORAGE ,WRITE_EXTERNAL_STORAGE
  • 在这样的构建方法中从您的代码中请求用户的许可,如下所示:

  • def build(self):
        if platform == 'android':
            from android.permissions import request_permissions, Permission
                request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
    
    
    然后您可以使用 kivy filechooser 或 kivymd filemanager 从手机存储中获取资源

    关于python - 从库 Python Kivy for Android 中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60066288/

    相关文章:

    javascript - 使用 jQuery 预加载图像

    python - Kivy 比例图像不起作用

    python - Scrapy 与 Mysql 出现此错误

    python - 将名字和姓氏标记为一个标记

    python - PyGTK/Gobject 等待挂起的任务

    android - 如何获取与SD卡上的音频添加的日期?

    python - FFMPEG Streaming,使用列表进行多个演示

    java - 如何让Activity继承方法

    android - 如何在软键盘 Android 的前面显示我的布局?

    java - 删除 .docx 文件中的图像