libaums icon indicating copy to clipboard operation
libaums copied to clipboard

Folder copy ??

Open mathan1079 opened this issue 3 years ago • 1 comments

How to copy folder on USB to internal directory ??

mathan1079 avatar Jul 02 '22 13:07 mathan1079

You could you the below to copy folders and contents inside 😁

currentFs.rootDirectory.listFiles().forEach {
                    if (it.name == "Folder name") {
                        val list = getListUsbFiles(it)
                        try {
                            list.forEach { rec ->
                                val dw = File("${filesDir}${rec.absolutePath}")
                                if (!File(dw.parent).exists()) {
                                    File(dw.parent).mkdirs()
                                }

                                copyFile(
                                    rec,
                                    dw.absolutePath
                                )
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
fun getListUsbFiles(parentDir: UsbFile): List<UsbFile> {
    val inFiles: ArrayList<UsbFile> = ArrayList()
    val files: Array<UsbFile> = parentDir.listFiles()
    for (file in files) {
        if (file.isDirectory) {
            inFiles.addAll(getListUsbFiles(file))
        } else {
            inFiles.add(file)
        }
    }
    return inFiles
}

fun copyFile(input: UsbFile, output: String) {
    val inputStream: InputStream = UsbFileInputStream(input)

    File(output).copyInputStreamToFile(inputStream)
}

fun File.copyInputStreamToFile(inputStream: InputStream) {
    this.outputStream().use { fileOut ->
        inputStream.copyTo(fileOut)
    }
}

naveenrobo avatar Jul 07 '22 13:07 naveenrobo

This is a feature of the example application even?!

magnusja avatar Nov 22 '22 09:11 magnusja