从 androidTest 读取 json 文件
read json file from androidTest
你好,我正在尝试读取 json 位于 androidTest 资源文件夹中的文件。我可以访问位于 Test/resources 资源文件夹中的 json 文件,但不能访问 androidTest/resources.
我使用以下方法获取 Test/resources 文件夹中的 json 文件。
private fun getJson(path: String): String {
val uri = this.javaClass.classLoader?.getResource(path)
val file = File(uri?.path ?: "")
return String(file.readBytes())
}
有什么方法可以访问 androidTest/resources 下的文件吗?
private fun getJson(fileName: Int): String {
val inputStream = InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(fileName)
val s = Scanner(inputStream).useDelimiter("\A")
return if (s.hasNext()) s.next() else ""
}
你好,我正在尝试读取 json 位于 androidTest 资源文件夹中的文件。我可以访问位于 Test/resources 资源文件夹中的 json 文件,但不能访问 androidTest/resources.
我使用以下方法获取 Test/resources 文件夹中的 json 文件。
private fun getJson(path: String): String {
val uri = this.javaClass.classLoader?.getResource(path)
val file = File(uri?.path ?: "")
return String(file.readBytes())
}
有什么方法可以访问 androidTest/resources 下的文件吗?
private fun getJson(fileName: Int): String {
val inputStream = InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(fileName)
val s = Scanner(inputStream).useDelimiter("\A")
return if (s.hasNext()) s.next() else ""
}