Reputation: 565
I followed a tutorial to setup reading JSON file from assets and parsing using GSON. This is all works well, but I want the user to also be able to edit the json file by making changes in the app.
I read that you cannot edit files in the assets folder but I don't know where I should put the file and how to reference it's path properly and online articles don't seem to be helping me. So, two questions:
1 ) Can I just move my JSON file into a new package in my MainActivity directory? Will this be included with the app APK?
2 ) How do I load this...? To load the current json from from assets I used
val mySquibsJSONfile = getApplication<Application>().assets.open("MySquibs.json")
What is the equivalent for file in an app folder like ./src/main/java/com/myname/myapp/json/MySquibsLocal.json
?
I've tried various versions of the below but can't get it working...
val mySquibsJSONfile = File("./src/main/java/com/myname/myapp/json/MySquibsLocal.json").inputStream()
Is it easy then to replace the json in the file?
Upvotes: 0
Views: 2600
Reputation: 30088
Anything that you package into the APK will be read-only, as the APK is a binary file, and is not expanded on installation. So just moving your assets to a new package will not help. And the 'src' directory does not appear in the APK.
You'll have to copy your assets to Android's file system at runtime, and make any modifications to that copy.
Android now has a lot of restrictions about where and how you can read and write files, so you'll probably want to read this very carefully: https://developer.android.com/training/data-storage
Upvotes: 1