Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App/Activity: Change file related Java libraries to Kotlin #39

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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