简体   繁体   English

Android 设备的图像无法显示,但 buildozer 目录中的图像可以显示在移动应用程序上

[英]Image from Android device cannot be displayed but Image inside the directory of buildozer can be displayed on the mobile application

I am doing the mobile application which shows an image from the storage of an android device on the mobile application.我正在做一个移动应用程序,它显示来自移动应用程序上的 android 设备存储的图像。

The issue I faced:我遇到的问题:

  • When opening an image file from the android device's storage on the mobile apps, the Image Widget shows a black screen.在移动应用程序上打开 android 设备存储中的图像文件时,图像小部件显示黑屏。 (refer to Figure 1) However, the image file is in the directory of the bulldozer, and it is able to display on the Image Widget. (参考图1) 但是,图片文件在推土机的目录下,可以在Image Widget上显示。 (refer to Figure 2) (参考图2)

The things I tried:我尝试过的事情:

  1. The apps will directly open Image from the android device's storage on the Image Widget after the user had selected the image.用户选择图像后,应用程序将直接在 Image Widget 上从 android 设备的存储中打开 Image。

  2. The code below shows, that the apps will open the Image from the android device's storage through the PIL.Image.open.下面的代码显示,应用程序将通过 PIL.Image.open 从 android 设备的存储中打开图像。 After that, the apps will use the PIL.Image to save the image inside the directory of the program.之后,应用程序将使用 PIL.Image 将图像保存在程序目录中。

Both ways brought out the same outcome (black screen).两种方式都产生了相同的结果(黑屏)。

Figure 1: Black Screen图 1:黑屏
图 1:黑屏

Figure 2: Image from the directory of buildozer图 2:来自 buildozer 目录的图像
图 2:来自 buildozer 目录的图像

Both images are the same below下面两张图是一样的

Image used: Raindowntothestreet.png使用的图片:Raindowntothestreet.png

Figure 3: Raindowntothestreet.png图 3:Raindowntothestreet.png
图 3:Raindowntothestreet.png

The code is below.代码如下。 showing 2)from the things I tried显示 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)

In the buildozer.spec,在 buildozer.spec 中,

  • the android's permission like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are already mentioned in the android.permissions. android.permissions 中已经提到了 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 等 android 的权限。 at below在下面

  • android.permissions = INTERNET,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE android.permissions = INTERNET,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE

  • requirements = kivy,python3,pillow,plyer,jnius,android,datetime要求 = kivy、python3、枕头、plyer、jnius、android、日期时间

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

Specially for your question, I created an example for working with images on Linux/Windows/macOS and Android .特别针对您的问题,我创建了一个在Linux/Windows/macOSAndroid上处理图像的示例。 Most importantly, all changes to widgets should occur in the main thread.最重要的是,对小部件的所有更改都应该发生在主线程中。 In my example, you can use both an image in a temporary folder and an existing image (I don't recommend doing this, it's better to work with a copy).在我的示例中,您可以同时使用临时文件夹中的图像和现有图像(我不建议这样做,最好使用副本)。 I created this example, because plyer does not work correctly with the selection of images at the moment.我创建了这个示例,因为plyer目前无法正确处理图像选择。 Cross-platform image picker 跨平台图像选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM