Reading JSON or text file in resource folder
Reading JSON or text file in resource folder
I am developing Kotlin app.
I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt).
Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader.
Unfortunately, my app cannot find the file.
Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them?
Thanks
1 Answer
1
If you want to read a file from res/raw
folder you can obtain InputStream
by R.raw.yourfile
id like this:
res/raw
InputStream
R.raw.yourfile
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/
folder and get InputStream
like this:
assets/
InputStream
assets.open("yourfile.txt")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment