问题
I visualize my app's data using google charts in the html file kept in assets folder of my android app. The html file is loaded in android WebView.
I use Firebase Cloud Messaging to silently update my android app's data.
Since the app needs to work offline too i need to persist the data. I can save it in shared preferences but i don't see a way for my javascript in html file to read android's shared preferences.
So, when the payload is received i want to update a .json file (in the assets folder) and wondering how to go about.
JSON:
[[a,b,c] , [1, 12, 24], [2, 45, 46]]
Payload:
[[a,0,1,53]]
Updated JSON:
[[a,b,c], [53, 12, 24], [2, 45, 46]]
first element in the second array is updated from 1 to 53
Thanks ^_^
回答1:
So, when the payload is received i want to update a .json file (in the assets folder)
That is not possible. Assets are read-only at runtime.
What you can do is write your new JSON to a file (e.g., getFilesDir()
). Then, adjust your logic that reads in the JSON to look for the file first, falling back to the asset if the file is not available.
回答2:
The assets
folder on your development machine is packaged into your APK file when you compile and release your app. On the device at run time, all resources in the assets
folder are read-only because they are loaded from the installed APK. If you want to store data on the machine, then you need to either use a SQLite database or write a local file. See Storage Options in the Android Developer Documentation for more details.
来源:https://stackoverflow.com/questions/39084717/update-json-file-in-android-assets-folder