Skip to content

Commit

Permalink
App/Activity: Change file related Java libraries to Kotlin
Browse files Browse the repository at this point in the history
This patch changes Java to Kotlin libraries.
Privious code is created with android-studio's Convert Java to Kotlin.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 authored and wooksong committed Jun 14, 2024
1 parent 5828ec9 commit 567b179
Showing 1 changed file with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import androidx.compose.ui.Modifier
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

// todo: Define DTO with generality and extensibility
data class ModelInfo(
Expand Down Expand Up @@ -78,39 +76,27 @@ class MainActivity : ComponentActivity() {
mService = null
}
}

private fun copyFilesToExternalDir() {
val am = resources.assets
var files: Array<String>? = null

try {
files = am.list("models/")
} catch (e: java.lang.Exception) {
Log.e(TAG, "#### Failed to get asset file list")
e.printStackTrace()
return
}

// Copy files into app-specific directory.
for (filename in files!!) {
try {
val inFile = am.open("models/$filename")
val outDir = getExternalFilesDir(null)!!.absolutePath
val outFile = File(outDir, filename)
val out: OutputStream = FileOutputStream(outFile)

val buffer = ByteArray(1024)
var read: Int
while ((inFile.read(buffer).also { read = it }) != -1) {
out.write(buffer, 0, read)
am.list("models/")?.let { fileArray ->
// Copy files into app-specific directory.
fileArray.forEach { fileName ->
try {
val inFile = am.open("models/$fileName")
inFile.use { stream ->
val outDir = getExternalFilesDir(null).toString()
File(outDir, fileName).outputStream().use {
stream.copyTo(it)
}
}
}
catch (e: IOException) {
Log.e(TAG, "Failed to copy file: $fileName")
e.printStackTrace()
return
}

inFile.close()
out.flush()
out.close()
} catch (e: IOException) {
Log.e(TAG, "Failed to copy file: $filename")
e.printStackTrace()
return
}
}
}
Expand Down

0 comments on commit 567b179

Please sign in to comment.