Algorithm to scan all files in android

I am trying to make an algorithm to scan all files in android but I am facing issues in adding directories to an array can anyone tell me why I am facing this issue?

The code I have currently written:

private fun getFiles() {
val mainDirectory = File(“storage/emulated/0/”)
val files = mainDirectory.listFiles()
val fileDirectories = Array(999){File("")}
var count = 0
if (files != null) {
for (file in files) {
if (".mp3") ||".MP3") ||".M4A") ||".m4a") ||".AAC") ||".aac")) {
databaseHandler.addSong(, file.path, file.toURI())
} else if (file.isDirectory) {
fileDirectories[count] = file

use a list instead (i.e. listOf(File("")) and then do if( { //something } else if (file.isDirectory) { fileDirectories.append(file) }
Note that you also won’t need the count variable anymore.

Thank you,
if you need any help contact me on Instagram at ashique_bava