【发布时间】:2022-06-13 01:18:36
【问题描述】:
我正在做一个移动应用程序,它在移动应用程序上显示来自 android 设备存储的图像。
我遇到的问题:
- 在移动应用程序上打开 android 设备存储中的图像文件时,图像小部件显示黑屏。 (参考图1) 但是,图片文件在推土机的目录下,可以在Image Widget上显示。 (参考图2)
我尝试过的事情:
-
用户选择图片后,应用会直接从安卓设备存储的Image Widget上打开Image。
-
下面的代码显示,应用程序将通过 PIL.Image.open 从 android 设备的存储中打开图像。之后,应用程序将使用 PIL.Image 将图像保存在程序目录中。
两种方式都产生了相同的结果(黑屏)。
下面两张图是一样的
使用的图片: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