Home > OS >  How to copy in Kotlin from assets folder to external or internal storage Android Studio
How to copy in Kotlin from assets folder to external or internal storage Android Studio

Time:11-14

I founded several code but all in Java... and I tried use ApplicationContext.assets but nothing

Some idea?

Thanks and regards

CodePudding user response:

This function will copy from assets folder and save it in external storage:

fun copyAssets(context: Context) {
    val assetManager: AssetManager = context.assets
    var files: Array<String>? = null
    try {
        files = assetManager.list("")

        if (files != null) for (filename in files) {
            var `in`: InputStream? = null
            var out: OutputStream? = null
            try {
                `in` = assetManager.open(filename)
                val outFile = File(context.getExternalFilesDir(null), filename)
                out = FileOutputStream(outFile)
                copyFile(`in`, out)
            } catch (e: IOException) {
                Log.e("tag", "Failed to copy asset file: $filename", e)
            } finally {
                if (`in` != null) {
                    try {
                        `in`.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
                if (out != null) {
                    try {
                        out.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        }

    } catch (e: IOException) {
        Log.e("tag", "Failed to get asset file list.", e)
    }

}

@Throws(IOException::class)
private fun copyFile(`in`: InputStream?, out: OutputStream) {
    val buffer = ByteArray(1024)
    var read: Int? = null
    while (`in`?.read(buffer).also({ read = it!! }) != -1) {
        read?.let { out.write(buffer, 0, it) }
    }
}
  • Related