Android Application

Kotlin 파일처리 : 디렉토리 관리 - 생성, 삭제, 목록보기

작성자 임베디드코리아 작성일23-09-28 01:11 조회1,561회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnGetDir"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Show App Directory Path"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginEnd="22dp"
        app:layout_constraintTop_toBottomOf="@+id/btnGetDir"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnMkdir"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Make Directory"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <Button
        android:id="@+id/btnFileList"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Load FileLists"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnMkdir" />

    <Button
        android:id="@+id/btnDeleteDir"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Delete Directory"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnFileList" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FileList"
        android:textSize="20sp"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var DirET: EditText
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        DirET = findViewById(R.id.editText)
        DirET.setOnClickListener  {
            DirET.setText(null)
        }

        binding.btnGetDir.setOnClickListener {
            toast(filesDir.toString())
        }

        binding.btnMkdir.setOnClickListener {

            makeDirectory(DirET.text.toString())
        }

        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
    }
}