Update json file in android assets folder

烂漫一生 提交于 2019-12-12 03:47:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!