使用kotlin读取android asstes 指定目录下的所有文件
可以使用以下代码来读取Android assets目录下指定目录下的所有文件:
fun getAssetFiles(context: Context, path: String): List<String> {
val assetManager = context.assets
val fileList = mutableListOf<String>()
try {
val files = assetManager.list(path)
for (file in files) {
val fullPath = "$path/$file"
if (file.contains(".")) {
fileList.add(fullPath)
} else {
fileList.addAll(getAssetFiles(context, fullPath))
}
}
} catch (e: IOException) {
e.printStackTrace()
}
return fileList
}
调用方法:
val files = getAssetFiles(context, "myfolder")
for (file in files) {
// do something with the file
}
其中,context为当前上下文,path为指定的目录。这个方法会返回一个包含所有文件路径的List。如果指定目录下还有子目录,会递归读取子目录下的文件
原文地址: https://www.cveoy.top/t/topic/cloY 著作权归作者所有。请勿转载和采集!