从 android kotlin 中的文件夹获取图像列表
Getting list of images from a folder in android kotlin
我正在尝试使用此功能从文件夹中获取图像列表
var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)
fun imageReader(root : File):ArrayList<File>{
val a : ArrayList<File> ? = null
val files = root.listFiles()
for (i in 0..files.size){
if (files[i].name.endsWith(".jpg")){
a?.add(files[i])
}
}
return a!!
}
但我有以下例外情况:
java.lang.ArrayIndexOutOfBoundsException:长度=3;索引=3
kotin.kotlinNullPointerException
我读到过这个问题,但不知道如何解决,
有什么帮助吗?
fun imageReader(root : File):ArrayList<File>{
val a : ArrayList<File> ? = null
val files = root.listFiles()
for (i in 0..files.size-1){
if (files[i].name.endsWith(".jpg")){
a?.add(files[i])
}
}
return a!!
}
For Null Pointer 你可能需要更改并传递 fullpath 而不是 path inside var list = imageReader(path)
.
错误
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(path)
右
var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)
编辑 1
我对函数做了一些更改并将其应用到 override fun onCreate 中,如下所示。
var gpath: String = Environment.getExternalStorageDirectory().absolutePath
var spath = "Download"
var fullpath = File(gpath + File.separator + spath)
Log.w("fullpath", "" + fullpath)
imageReaderNew(fullpath)
函数
fun imageReaderNew(root: File) {
val fileList: ArrayList<File> = ArrayList()
val listAllFiles = root.listFiles()
if (listAllFiles != null && listAllFiles.size > 0) {
for (currentFile in listAllFiles) {
if (currentFile.name.endsWith(".jpeg")) {
// File absolute path
Log.e("downloadFilePath", currentFile.getAbsolutePath())
// File Name
Log.e("downloadFileName", currentFile.getName())
fileList.add(currentFile.absoluteFile)
}
}
Log.w("fileList", "" + fileList.size)
}
}
Logcat输出
W/fullpath: /storage/emulated/0/Download
E/downloadFilePath: /storage/emulated/0/Download/download.jpeg
E/downloadFileName: download.jpeg
E/downloadFilePath: /storage/emulated/0/Download/images.jpeg
E/downloadFileName: images.jpeg
E/downloadFilePath: /storage/emulated/0/Download/images (1).jpeg
E/downloadFileName: images (1).jpeg
上面的答案是正确的,但是它将 a
声明为 null
然后在循环中使用 null
保存。因此它检测图像但不将它们添加到列表和列表 returns null.
fun imageReader(root: File): ArrayList < File > {
val a: ArrayList < File > = ArrayList()
if (root.exists()) {
val files = root.listFiles()
if (files.isNotEmpty()) {
for (i in 0..files.size - 1) {
if (files[i].name.endsWith(".jpg")) {
a.add(files[i])
}
}
}
}
return a!!
}
我正在尝试使用此功能从文件夹中获取图像列表
var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)
fun imageReader(root : File):ArrayList<File>{
val a : ArrayList<File> ? = null
val files = root.listFiles()
for (i in 0..files.size){
if (files[i].name.endsWith(".jpg")){
a?.add(files[i])
}
}
return a!!
}
但我有以下例外情况:
java.lang.ArrayIndexOutOfBoundsException:长度=3;索引=3
kotin.kotlinNullPointerException
我读到过这个问题,但不知道如何解决,
有什么帮助吗?
fun imageReader(root : File):ArrayList<File>{
val a : ArrayList<File> ? = null
val files = root.listFiles()
for (i in 0..files.size-1){
if (files[i].name.endsWith(".jpg")){
a?.add(files[i])
}
}
return a!!
}
For Null Pointer 你可能需要更改并传递 fullpath 而不是 path inside var list = imageReader(path)
.
错误
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(path)
右
var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)
编辑 1
我对函数做了一些更改并将其应用到 override fun onCreate 中,如下所示。
var gpath: String = Environment.getExternalStorageDirectory().absolutePath
var spath = "Download"
var fullpath = File(gpath + File.separator + spath)
Log.w("fullpath", "" + fullpath)
imageReaderNew(fullpath)
函数
fun imageReaderNew(root: File) {
val fileList: ArrayList<File> = ArrayList()
val listAllFiles = root.listFiles()
if (listAllFiles != null && listAllFiles.size > 0) {
for (currentFile in listAllFiles) {
if (currentFile.name.endsWith(".jpeg")) {
// File absolute path
Log.e("downloadFilePath", currentFile.getAbsolutePath())
// File Name
Log.e("downloadFileName", currentFile.getName())
fileList.add(currentFile.absoluteFile)
}
}
Log.w("fileList", "" + fileList.size)
}
}
Logcat输出
W/fullpath: /storage/emulated/0/Download
E/downloadFilePath: /storage/emulated/0/Download/download.jpeg
E/downloadFileName: download.jpeg
E/downloadFilePath: /storage/emulated/0/Download/images.jpeg
E/downloadFileName: images.jpeg
E/downloadFilePath: /storage/emulated/0/Download/images (1).jpeg
E/downloadFileName: images (1).jpeg
上面的答案是正确的,但是它将 a
声明为 null
然后在循环中使用 null
保存。因此它检测图像但不将它们添加到列表和列表 returns null.
fun imageReader(root: File): ArrayList < File > {
val a: ArrayList < File > = ArrayList()
if (root.exists()) {
val files = root.listFiles()
if (files.isNotEmpty()) {
for (i in 0..files.size - 1) {
if (files[i].name.endsWith(".jpg")) {
a.add(files[i])
}
}
}
}
return a!!
}