0

我正在尝试通过 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

这是我的文件-

测试.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()
}
}

构建.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'
}
4

1 回答 1

1

即使它在模拟器上运行,Android 应用程序仍然无法访问您的开发机器上的文件。它们实际上是两个独立的设备。

但是,您 Python 源目录中的任何数据文件都将被复制到您的 APK 中并解压缩到 Android 设备上。然后,您可以使用相对于 的路径访问它,如此__file__所示。

于 2020-12-23T14:32:57.363 回答