mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-02-16 20:00:36 +01:00
add directory copying on SD card
This commit is contained in:
parent
f8e855ec15
commit
7d08cd8653
@ -36,41 +36,67 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
|
|||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
private fun copy(source: File, destination: File) {
|
private fun copy(source: File, destination: File) {
|
||||||
if (source.isDirectory) {
|
if (source.isDirectory) {
|
||||||
if (!destination.exists() && !destination.mkdirs()) {
|
copyDirectory(source, destination)
|
||||||
|
} else {
|
||||||
|
copyFile()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun copyDirectory(source: File, destination: File) {
|
||||||
|
if (!destination.exists()) {
|
||||||
|
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
|
||||||
|
val document = Utils.getFileDocument(context, destination.absolutePath)
|
||||||
|
document.createDirectory(destination.name)
|
||||||
|
} else if (!destination.mkdirs()) {
|
||||||
throw IOException("Could not create dir " + destination.absolutePath)
|
throw IOException("Could not create dir " + destination.absolutePath)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val children = source.list()
|
val children = source.list()
|
||||||
for (child in children) {
|
for (child in children) {
|
||||||
copy(File(source, child), File(destination, child))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
val directory = destination.parentFile
|
|
||||||
if (!directory.exists() && !directory.mkdirs()) {
|
|
||||||
throw IOException("Could not create dir " + directory.absolutePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
val inputStream = FileInputStream(source)
|
|
||||||
var out: OutputStream?
|
|
||||||
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
|
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
|
||||||
var document = Utils.getFileDocument(context, destination.absolutePath)
|
var document = Utils.getFileDocument(context, destination.absolutePath)
|
||||||
document = document.createFile("", destination.name)
|
document = document.createFile("", child)
|
||||||
out = context.contentResolver.openOutputStream(document.uri)
|
|
||||||
} else {
|
|
||||||
out = FileOutputStream(destination)
|
|
||||||
}
|
|
||||||
|
|
||||||
val buf = ByteArray(1024)
|
val inputStream = FileInputStream(File(source, child))
|
||||||
var len: Int
|
val out = context.contentResolver.openOutputStream(document.uri)
|
||||||
while (true) {
|
copyStream(inputStream, out)
|
||||||
len = inputStream.read(buf)
|
} else {
|
||||||
if (len <= 0)
|
copy(File(source, child), File(destination, child))
|
||||||
break
|
|
||||||
out!!.write(buf, 0, len)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun copyFile(source: File, destination: File) {
|
||||||
|
val directory = destination.parentFile
|
||||||
|
if (!directory.exists() && !directory.mkdirs()) {
|
||||||
|
throw IOException("Could not create dir " + directory.absolutePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
val inputStream = FileInputStream(source)
|
||||||
|
val out: OutputStream?
|
||||||
|
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
|
||||||
|
var document = Utils.getFileDocument(context, destination.absolutePath)
|
||||||
|
document = document.createFile("", destination.name)
|
||||||
|
out = context.contentResolver.openOutputStream(document.uri)
|
||||||
|
} else {
|
||||||
|
out = FileOutputStream(destination)
|
||||||
|
}
|
||||||
|
|
||||||
|
copyStream(inputStream, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun copyStream(inputStream: InputStream, out: OutputStream?) {
|
||||||
|
val buf = ByteArray(1024)
|
||||||
|
var len: Int
|
||||||
|
while (true) {
|
||||||
|
len = inputStream.read(buf)
|
||||||
|
if (len <= 0)
|
||||||
|
break
|
||||||
|
out?.write(buf, 0, len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onPostExecute(success: Boolean) {
|
override fun onPostExecute(success: Boolean) {
|
||||||
val listener = mListener?.get() ?: return
|
val listener = mListener?.get() ?: return
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user