Hanbit the Developer

[Kotlin] Get URI from byte array in android 본문

Mobile/Android

[Kotlin] Get URI from byte array in android

hanbikan 2021. 8. 19. 23:34
fun getUriFromByteArray(byteArray: ByteArray): Uri{
    // Set directory
    val dir = File(requireContext().getExternalFilesDir(null).toString() + "/YOUR_DIR")
    if(! dir.exists()){
        dir.mkdir()
    }

    // Create dummy file
    val file = if(url.endsWith(".mp4")){
        File.createTempFile("post_media", ".mp4", dir)
    }else{
        File.createTempFile("post_media", ".webm", dir)
    }

    // Write bytes to the file
    val os = FileOutputStream(file)
    os.write(byteArray)
    os.close()

    return Uri.fromFile(file)
}