Home > Blockchain >  How to get File object from assets?
How to get File object from assets?

Time:07-24

I have a markdown file in my assets folder. I am using a markdown viewer which takes java.io.File Object as an argument to parse the file on screen.

But i don't know how to get a File Object from a file in assets folder. Is there a way i can achieve this. I tried URI way but it failed.

This is my code snippet which may help

val markDown = File("file:///asset/GalacticAstronomy.md")
        if (markDown.exists()) {
            Toast.makeText(baseContext, "File exist", Toast.LENGTH_SHORT).show()
        } else Toast
            .makeText(baseContext, "File doesn't exist", Toast.LENGTH_SHORT).show()
        setContent {
            MaterialTheme {
                MarkDown(
                    file = markDown,
                    modifier = Modifier.fillMaxSize()
                )
            }
        }

I am not trying to read the file, So using assets.open("file") isn't going to work

CodePudding user response:

But i don't know how to get a File Object from a file in assets folder

That is not possible, as it is not a file.

I am not trying to read the file, So using assets.open("file") isn't going to work

You do not have a choice. Either:

  • Use a better markdown library, one that accepts an InputStream, or
  • Copy the asset to a file yourself (e.g., in getCacheDir()) using assets.open()), then use the resulting file with your existing library
  • Related