【问题标题】:Image from Android device cannot be displayed but Image inside the directory of buildozer can be displayed on the mobile applicationAndroid 设备的图像无法显示,但 buildozer 目录中的图像可以显示在移动应用程序上
【发布时间】:2022-06-13 01:18:36
【问题描述】:

我正在做一个移动应用程序,它在移动应用程序上显示来自 android 设备存储的图像。

我遇到的问题:

  • 在移动应用程序上打开 android 设备存储中的图像文件时,图像小部件显示黑屏。 (参考图1) 但是,图片文件在推土机的目录下,可以在Image Widget上显示。 (参考图2)

我尝试过的事情:

  1. 用户选择图片后,应用会直接从安卓设备存储的Image Widget上打开Image。

  2. 下面的代码显示,应用程序将通过 PIL.Image.open 从 android 设备的存储中打开图像。之后,应用程序将使用 PIL.Image 将图像保存在程序目录中。

两种方式都产生了相同的结果(黑屏)。

图 1:黑屏

图 2:来自 buildozer 目录的图片

下面两张图是一样的

使用的图片:Raindowntothestreet.png

图 3:Raindowntothestreet.png

代码如下。 显示 2)从我尝试过的事情来看

import os

os.environ['KIVY_IMAGE'] = 'pil,sdl2'
class MainPage(FloatLayout):
    # Getting the folder address of the program located
    running_program_path = os.path.abspath(os.path.dirname(__file__))
    path_file_notation = ""

    def __init__(self, **kwargs):
        ......

        # def image and label as ""
        self.image_selected.source = ""
        self.label_selected.text = ""
        
        # To show the Image from the directory of the buildozer
        self.image_selected.source = "Raindowntothestreet.png"



    def selected(self, selection):
        if selection:
            # opening selected file
            img = PIL_Image.open(selection[0])

            # create directory folder for the files
            # path_notation = self.running_program_path
            # if not os.path.isdir(path_notation):
            #     os.makedirs(path_notation)

            # creating temporary file name with datetime, string, random
            currentDateTime = datetime.now()
            currentDate = currentDateTime.strftime("%Y%m%d")
            currentTime = currentDateTime.strftime("%H%M%S")
            alphabet1 = random.choice(string.ascii_letters)
            alphabet2 = random.choice(string.ascii_letters)

            # create a temporary file for the selected file
            self.path_file_notation = f"temp" + currentDate + currentTime + alphabet1 + 
                                      alphabet2 + f".png"

            # save the selected at the program directory
            img.save(self.path_file_notation)

            # showing the file on the Image Widget
            self.image_selected.source = self.path_file_notation

            # showing the path of file located
            self.label_selected.text = "Source: " + selection[0]
            print(self.image_selected.source)

在 buildozer.spec 中,

  • android.permissions 中已经提到了 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 等 android 的权限。在下面

  • android.permissions = INTERNET,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE

  • 要求 = kivy,python3,pillow,plyer,jnius,android,datetime

  • source.include_exts = py,png,jpg,kv,atlas

【问题讨论】:

  • 是否要显示位于应用程序文件夹中的图像?
  • 嗨@Neizvestnyj,我想显示来自Android设备内部存储的图像,而不是位于应用程序文件夹中的图像。
  • 你能发布可运行的例子吗?
  • 嗨@Neizvestnyj,您可以从这里查看代码。链接:github.com/yj0mun/Showing_Image
  • 你的安卓版本是多少?

标签: python android kivy mobile-application


【解决方案1】:

特别针对您的问题,我创建了一个在 Linux/Windows/macOS 和 Android 上处理图像的示例。最重要的是,对小部件的所有更改都应该发生在主线程中。在我的示例中,您可以同时使用临时文件夹中的图像和现有图像(我不建议这样做,最好使用副本)。我创建了这个示例,因为plyer 目前无法正确处理图像选择。 Cross-platform image picker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多