binding.btnFileList.setOnClickListener {
binding.textView.text = null
val path = File(filesDir.toString())
val files = path.listFiles()
var strFileList: String? = "App Directory File Lists\n"
for(file in files){
strFileList += "->" + file.name + "\n"
}
binding.textView.text = strFileList
}
binding.btnDeleteDir.setOnClickListener {
val files = File(filesDir.toString()).listFiles()
val selectedItemIndex = ArrayList<Int>()
var filesName = Array(files.size) { item -> "" }
for(i in files.indices){
filesName[i] = files[i].name
}
val builder = AlertDialog.Builder(this)
.setTitle("삭제할 디렉토리 선택")
.setMultiChoiceItems(filesName, null){ dialogInterface: DialogInterface, i: Int, b: Boolean ->
if(b){
selectedItemIndex.add(i)
}else if(selectedItemIndex.contains(i)){
selectedItemIndex.remove(i)
}
}
.setPositiveButton("삭제"){ dialogInterface: DialogInterface, i: Int ->
for(i in selectedItemIndex.indices){
val file = File("$filesDir/${filesName[selectedItemIndex[i]]}")
file.delete()
}
}
val dialog = builder.create()
dialog.show()
}
}
fun makeDirectory(folderName:String){
val path = File("$filesDir/$folderName")
if(!path.exists()){
path.mkdirs()
}else{
toast("$folderName is exists")
}
}
fun toast(message:String){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
------------------------------------------------------------------------------------
build.gradle(Module) : build.gradle.kts
-------------------------------------------------------------------------------------
// 안드로이드 스튜디오 4.0 이상
android {
...
buildFeatures {
viewBinding = true
}
}