libaums
libaums copied to clipboard
Folder copy ??
How to copy folder on USB to internal directory ??
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)
}
}
This is a feature of the example application even?!