我尝试通过Python在Android Studio中打开一个文本文件。我在IDE中配置了chaquo插件,可以运行一个简单的程序(如“Hello World”),没有任何问题。但是,当我尝试打开外部文件时,我收到此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: com.chaquo.python.PyException: FileNotFoundError: [Errno 2] No such file or directory: 'C:/.../app/src/main/python/text.txt'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
我已经包含了我的build.gradle文件,其中包含为安装chaquopy插件而添加的代码作为参考,我使用了https://chaquo.com/chaquopy/doc/current/android.html
这是我的文件-
test.py:
from os.path import dirname, join
def tt():
filename = join(dirname(__file__), "text.txt")
f = open(filename, "r")
print(f.read())
return filename
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import android.widget.TextView
import com.chaquo.python.Python
import com.chaquo.python.android.AndroidPlatform
class MainActivity : AppCompatActivity() {
private lateinit var test: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test = findViewById(R.id.t)
initPython()
}
private fun initPython() {
if (!Python.isStarted()) {
Python.start(AndroidPlatform(this))
}
val python = Python.getInstance()
val pythonFile = python.getModule("test")
val obj = pythonFile.callAttr("tt")
test.text = obj.toString()
}
}
build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.chaquo.python'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId ""
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
sourceSets {
main {
python {
srcDirs = ["src/main/python"]
python.srcDirs = ["src/main/python"]
}
}
}
python {
buildPython "C:/.../AppData/Local/Programs/Python/Python36-32/python.exe"
buildPython "python3"
}
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility kotlin_version
targetCompatibility kotlin_version
}
kotlinOptions {
jvmTarget = '1.8'
}
ndkVersion '21.1.6352462'
}
1条答案
按热度按时间dvtswwa31#
即使Android应用运行在模拟器上,它仍然无法访问开发机器上的文件,它们实际上是两个独立的设备。
但是,Python源目录中的任何数据文件都将被复制到APK中并提取到Android设备上,然后您可以使用相对于
__file__
的路径访问它,如here所示。