Kivy 应用程序无法 find/read 我的 txt 文件在根目录 Android

Kivy app can't find/read my txt file in root directory Android

我创建了一个只读取我的 txt 并使用标签将内容打印到屏幕上的应用程序,但是当我 运行 android 上的应用程序时它找不到我的 txt, windows 工作正常。这是我的代码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout

class MainApp(App):
    def build(self):
        fl = FloatLayout()
        fl.add_widget(Label(text=open("settings.txt", "r").read(), halign="center"))
        return fl

MainApp().run()

怎么了?而且我不会使用 ConfigParser 因为它已经在我的项目中使用了,这只是我为这个问题创建的示例代码。

任何帮助将不胜感激

open("settings.txt", "r").read()

此代码尝试打开当前目录中名为“settings.txt”的 already-existing 文件。您还没有解释失败的性质,但大概不存在。您也没有解释为什么您认为该文件会存在,因此我们无法指出任何具体错误。

一般的解决办法是,做一些事情让那个文件存在。也许您在打包的文件类型中忽略了它。

在您的 buildozer.spec 文件中修改行:

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

至:

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

并确认您没有在 spec 文件的其他地方排除 txt 文件。