无法显示来自Android设备的图像,但是可以在移动应用程序上显示Buildozer目录中的图像
我正在进行移动应用程序,该应用程序显示了移动应用程序上Android设备的存储图像。
我面临的问题:
- 从Android设备在移动应用程序上打开图像文件时,图像小部件显示了黑屏。 (请参阅图1)但是,图像文件位于推土机的目录中,并且能够在图像小部件上显示。 (请参阅图2)
我尝试的东西:
这些应用程序将在用户选择图像后直接从Android设备的存储中打开图像。
下面的代码显示,应用程序将通过pil.image.open从Android设备的存储中打开图像。之后,应用程序将使用pil.image将图像保存在程序目录中。
两种方式都带来了相同的结果(黑屏)。
相同
这两个图像均与下面的图像
: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中已经提到了 。在下面
android.permissions = Internet,read_external_storage,write_external_storage
需求= Kivy,Python3,Pillow,Plyer,Plyer,Jnius,android,android,datetime
source.include_exts = py,py,png,jpg,kv,kv,atlas
I am doing the mobile application which shows an image from the storage of an android device on the mobile application.
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. (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. (refer to Figure 2)
The things I tried:
The apps will directly open Image from the android device's storage on the Image Widget after the user had selected the image.
The code below shows, that the apps will open the Image from the android device's storage through the PIL.Image.open. After that, the apps will use the PIL.Image to save the image inside the directory of the program.
Both ways brought out the same outcome (black screen).
Figure 2: Image from the directory of buildozer
Both images are the same below
Image used: Raindowntothestreet.png
Figure 3: Raindowntothestreet.png
The code is below.
showing 2)from the things I tried
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,
the android's permission like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are already mentioned in the android.permissions. at below
android.permissions = INTERNET,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE
requirements = kivy,python3,pillow,plyer,jnius,android,datetime
source.include_exts = py,png,jpg,kv,atlas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
特别是对于您的问题,我创建了一个示例,用于使用 Linux/Windows/MacOS 和 Android 上的图像。最重要的是,主线程中应发生所有对小部件的更改。在我的示例中,您可以同时使用临时文件夹中的图像和现有图像(我不建议这样做,最好使用副本)。我创建了此示例,因为
plyer
目前与图像的选择不正确。cross-platform映像picker
Specially for your question, I created an example for working with images on Linux/Windows/macOS and Android. 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.Cross-platform image picker