How to get the android Path string to a file on Assets folder?

前端 未结 4 719
野的像风
野的像风 2020-11-27 03:29

I need to know the string path to a file on assets folder, because I\'m using a map API that needs to receive a string path, and my maps must be stored on a

4条回答
  •  忘掉有多难
    2020-11-27 04:03

    Just to add on Jacek's perfect solution. If you're trying to do this in Kotlin, it wont work immediately. Instead, you'll want to use this:

    @Throws(IOException::class)
    fun getSplashVideo(context: Context): File {
        val cacheFile = File(context.cacheDir, "splash_video")
        try {
            val inputStream = context.assets.open("splash_video")
            val outputStream = FileOutputStream(cacheFile)
            try {
                inputStream.copyTo(outputStream)
            } finally {
                inputStream.close()
                outputStream.close()
            }
        } catch (e: IOException) {
            throw IOException("Could not open splash_video", e)
        }
        return cacheFile
    }
    

提交回复
热议问题