Android how to copy list files selected using AsyncTask and show status current percent copy file

Viewed 21

I'm picking a file from the gallery and copying it to a specified folder. While copying I'm showing a ProgressDialog, I'm doing this with AsyncTask.

I'm trying to show the progress of the file being copied with percentage. How can I get the current progress of the file being copied and display it as percentage ??? My code for copy file:

// Copy list file selected
for(i in 0 until arrayListCopyMoveFile.size){
                if(arrayListCopyMoveFile.get(i).check){
                    copyFileOrDirectory(arrayListCopyMoveFile.get(i).file.path,path)
                }
            }
    fun copyFileOrDirectory(srcDir: String?, dstDir: String?) {
        try {
            val src = File(srcDir)
            val dst = File(dstDir, src.name)
            if (src.isDirectory) {
                val files = src.list()
                val filesLength = files.size
                for (i in 0 until filesLength) {
                    val src1 = File(src, files[i]).path
                    val dst1 = dst.path
                    copyFileOrDirectory(src1, dst1)
                }
            } else {
                copyFile(src, dst)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @Throws(IOException::class)
    fun copyFile(sourceFile: File?, destFile: File) {
        if (!destFile.parentFile.exists()) destFile.parentFile.mkdirs()
        if (!destFile.exists()) {
            destFile.createNewFile()
        }
        var source: FileChannel? = null
        var destination: FileChannel? = null
        try {
            source = FileInputStream(sourceFile).getChannel()
            destination = FileOutputStream(destFile).getChannel()
            destination.transferFrom(source, 0, source.size())
        } finally {
            if (source != null) {
                source.close()
            }
            if (destination != null) {
                destination.close()
            }
        }
    } ```
0 Answers
Related