Copy files from a folder of SD card into another folder of SD card

前端 未结 5 842
日久生厌
日久生厌 2020-12-13 10:22

Is it possible to copy a folder present in sdcard to another folder present the same sdcard programmatically ??

If so, how to do that?

5条回答
  •  情话喂你
    2020-12-13 11:02

    Kotlin code

    fun File.copyFileTo(file: File) {
        inputStream().use { input ->
            file.outputStream().use { output ->
                input.copyTo(output)
            }
        }
    }
    
    fun File.copyDirTo(dir: File) {
        if (!dir.exists()) {
            dir.mkdirs()
        }
        listFiles()?.forEach {
            if (it.isDirectory) {
                it.copyDirTo(File(dir, it.name))
            } else {
                it.copyFileTo(File(dir, it.name))
            }
        }
    }
    

提交回复
热议问题