Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[Kotlin] Asset ํŒŒ์ผ ์ฝ์–ด์„œ sdcard ์ €์žฅํ•˜๊ธฐ ๋ณธ๋ฌธ

Android

[Kotlin] Asset ํŒŒ์ผ ์ฝ์–ด์„œ sdcard ์ €์žฅํ•˜๊ธฐ

MK_____ 2022. 10. 5. 15:31

์•ฑ ์ฒ˜์Œ ์‹คํ–‰๋ ๋•Œ assets ํŒŒ์ผ ์ฝ์–ด์„œ ๋ณต์‚ฌํ•ด์„œ ํ•„์š”ํ•œ file๋“ค sdcard์— copy & paste

 

 

package kr.co.voicecaddie.bodycam

import android.app.Application
import android.content.Context
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import dagger.hilt.android.HiltAndroidApp
import kr.co.voicecaddie.camera.CameraDefine
import kr.co.voicecaddie.common.NetworkUtils
import kr.co.voicecaddie.common.VCDebugLog
import splitties.toast.toast
import java.io.*

@HiltAndroidApp
class BodyCamApp : Application() {
    private val logTag = BodyCamApp::class.simpleName ?: ""

    companion object {
        lateinit var prefs: SharedPreferences
    }

    override fun onCreate() {
        prefs = SharedPreferences(applicationContext)
        super.onCreate()

        VCDebugLog.i(logTag, "onCreate-()")

        NetworkUtils.registerWifiConnected(applicationContext) { ssid ->
            if (ssid?.startsWith(CameraDefine.SSID_PREFIX) == true) {
                appCameraSSID.postValue(ssid)
                appCameraConnected.postValue(true)
            } else {
                appCameraConnected.postValue(false)
            }
        }

        // asset ํด๋”์— ์žˆ๋Š” ํŒŒ์ผ -> sdcard ๋ณต์‚ฌ
        copyAssets()
    }

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

        try {
            files = assetManager.list("datasets/") // asset ํด๋”์˜ ๊ฒฝ๋กœ
        } catch (e: IOException) {
            VCDebugLog.e("Failed to get asset file list. e => $e")
        }

        if (files != null) for (filename in files) {
            var `in`: InputStream? = null
            var out: OutputStream? = null

            try {
                VCDebugLog.d(logTag, "filename: $filename")
                `in` = assetManager.open("datasets/$filename") // asset ํด๋” ๊ฒฝ๋กœ์— ์žˆ๋Š” ํŒŒ์ผ ์ฝ๊ธฐ

                // sdcard ํด๋” ์œ ๋ฌด ํ™•์ธ
                val dir = File(
                    Environment.getExternalStorageDirectory(), "/BodyCam/datasets")
                if (!dir.exists()) {
                    dir.mkdir()
                }
                out = FileOutputStream("${Environment.getExternalStorageDirectory().absolutePath}/BodyCam/datasets/$filename") // sdcard ์ €์žฅ ๊ฒฝ๋กœ
                copyFile(`in`, out)
                VCDebugLog.d(logTag, "Success copy file")
            } catch (e: IOException) {
                VCDebugLog.e("Failed to copy asset file. filename => $filename, e => $e")
            } finally {
                if (`in` != null) {
                    try {
                        `in`.close()
                        //`in` = null
                    } catch (e: IOException) {
                    }
                }
                if (out != null) {
                    try {
                        out.flush()
                        out.close()
                        //out = null
                    } catch (e: IOException) {
                        // nothing
                    }
                }
            }
        }
    }

    fun copyFile(`in`: InputStream, out: OutputStream) {
        val buffer = ByteArray(1024)
        var read: Int
        while (`in`.read(buffer).also { read = it } != -1) {
            out.write(buffer, 0, read)
        }
    }
}

 

ํ˜„์žฌ ์ฝ”๋“œ๋Š” ์•ฑ ๊ตฌ๋™์ด ๋ ๋•Œ๋งˆ๋‹ค assets ํŒŒ์ผ์„ sdcard์— ์กด์žฌํ•˜๋˜ ์•ˆํ•˜๋˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์ง€ ์•Š๊ณ , 

๋ฐ”๋กœ ํŒŒ์ผ์„ copy & paste (๋ฎ์–ด์“ฐ๊ธฐ) ๋˜๊ฒŒ ๋˜์–ด์žˆ๋‹ค.

 

TODO :: ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ์ž‘์—… ํ•„์š”!