본문 바로가기

AOS - Kotlin

[android/kotlin] Media Store 이용해 파일 저장

반응형
SMALL

내장메모리/Download 영역에 txt 파일 저장

 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val contentValues = ContentValues()
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain")
    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "Download")

    val resolver = contentResolver
    val uri = resolver.insert(MediaStore.Files.getContentUri("external"), contentValues)

    try {
        val writeFile = "안드로이드"
        resolver.openOutputStream(uri!!)!!.write(writeFile.toByteArray())
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
}
반응형
LIST