How do I write to localStorage from Java on Android - android

I am looking into the http://developer.android.com/reference/android/webkit/WebStorage.html API but it does not support writing to localStorage from Java. What I would like to do is to store data from the Java side of an android application to be read by the JavaScript side of the app.
For instance, in JavaScript I can use localStorage.getItem(), localStorage.setItem(). However, I cannot find similar API to accomplish the task from Java. Is there a workaround or it has not been implemented by the Android platform yet?
Any help would be appreciated!

There are many ways to store data in an Android application, as detailed in the Storage Options guide:
Shared Preferences - simple key/value pairs specific to your application. This is probably the closest to WebStorage.
Internal Storage - read/write files
External Storage - SD card file storage
Databases - better for large amounts of structured data
Network - obviously, you can store/retrieve data remotely if needed

Related

What is best way to store large data in local for iOS/Android

I'm working on an app and I have tried to use local storage but I'm not sure if it will be OK for my project.
I need to store really large data every second in my local database, and at the end of the day send to my server. This can be large data because every second write my GPS coordinates.
What is best: SQLite, local storage or websql?
I work using cordova.
I appreciate for your help.
Regards
I am an Android developer so this is how it works in Android :
Shared Preferences - simple key/value pairs specific to your application. This is probably the closest to WebStorage - only for small amount of data.
Internal Storage - read/write files(only the app can access it)
External Storage - SD card file storage(app + other apps including the user) can access it).
Databases (SQLite) - better for large amounts of structured data
Network - obviously, you can store/retrieve data remotely if needed (like Firebase).
What would I choose? if data is structured then I would choose SQLite.
More detailed information :
https://developer.android.com/training/data-storage
cordova-sqlite-storage is Native SQLite component with API based.
It will easily handle large data and easy for CRUD operation.
Yes you can use cordova-sqlite-storage for large data inputs. But I think it is also advisable not to stress your device with so much data in it because mobile is just mobile with so many limitations in terms of hardware. Maybe you can just set a treshold of data size and after reaching it, send it to your server then clear your database.
The point is don't stress you device, use minimum data as much as possible to provide a clean app for the users.

Cordova/Phonegap/Ionic App - local store of remote images

I'm building an app using Ionic/AngularJS and would pull down remote data (JSON) on app start.
For examples sake:
[{"id":1,"name":"Retriever","image":"http://server.com/images/image1.jpg"},
{"id":2,"name":"Collie","image":"http://server.com/images/image2.jpg"},
{"id":3,"name":"Poodle","image":"http://server.com/images/image3.jpg"}]
This data has a number of images -
What is the recommended way to store these locally on the device (and update the filepaths in the JSON as this is used for filtering display data on views) allowing for offline viewing?
Solution 1
Use $cordovaFile service from ngCordova - http://ngcordova.com/docs/ (looks like you will need downloadFile method of this service)
Solution 2
Use this Cordova plugin - https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md (This plugin implements a File API allowing read/write access to files residing on the device.)
Essentially, solution 1 is just a Angular wrapper for this File API.
Solution 3
Store images in Local Storage, but it's very limited due to you have size limit of like 5 Mb. Also storing images in storage like this is generally bad idea.
Solution 4
Use WebSQL as alternative to Local Storage.
I personally would go with solution 1.

LocalStorage vs Sqlite database

I have a hybrid application which runs on Android and ios platform, I have been using "localStorage" as data storage.
I also heard sqlite as well, Is there any other local database that I can use for mobile storage?
I would like to know prop and cons in these technologies.
I would really appreciate if anyone can clarify about these technologies.
You can manage data or storage in Android by following ways,
Shared Preferences
Internal Storage
External Storage
SQLite Databases
Network Connection
You can manage the storage or data in iOS by following ways,
Property lists
SQLite database
Core Data
As you say that you have a hybrid application which is for Android and ios you should use SQLite database, because
It is light weight
Comfortable with both Android and iOS [ Cross platform ]
capacity to Handle huge data
The handling of code, manipulating the code is much easy with sqlite.
So, its my opinion to u that you should use SQLite as an database for hybrid application, it will be easy for you. If you are doing application only for iOS then core date can be another good option since you doing both one SQLite is best.
As far as we consider speed and performance , SQLITE is best according to docs.
but you can use xml data storage as well but you will find a little bit slow than sqlite.
Your options are limited. Shared Preferences, local storage, sqlite and network are your only options.
Everything you need to know about your options can be found at the official Storeage Options docs.
I recommend that if you only need to save user options and states and not lots of data then you only need to use Shared Preferences. You can even save JSON in the shared preferences if you don't want to setup a sqlite database.
And remember a user can wipe app data from the app settings, if your worried about the data being wiped then the only option is network or a combination with network.

Storing data and files

I am porting an application originally written for WP7 and just need to know how to do something under Mono for Android which I could do on WP7 using IsolatedStorage.
For simple key/value data such as the username and password used to login I am using SharedPreferences and I think that this will work fine, however I read an XML file from a web service and need to be able to store this file locally, be able to update its contents and ultimately upload it back to the web server.
Under WP7 the code would serialize the XML and then save this to application settings - I guess I can do something very similar in Android but the question is this the best way to store an XML data file?
Thanks
You can do almost the same thing as you would on the WP, as the same .net libraries for xml and serialization should be available on Mono for Android.
You can then also save it to isolated storage or wherever you like.
You can use one of the common java libraries to work with XML, such as JDOM or SAX. You can then write this to android storage as per Maxim's comment link above.
You can write the XML as a string to a file in the Internal Storage or the SD Card. Check the Developer Documentation for more information: http://developer.android.com/guide/topics/data/data-storage.html

Storage of Static data within android app

I am currently developing a simple info app on various university campuses. I want the app to operate predominantly in an offline state thus I want to store all my information locally. The data within the app will not be subject to any change thus I was wondering what the best (practice) method was to store such data? Basically should I be storing the info in a SQLite db, java file or xml?
The answer depends on your requirements, but because of the built-in integration with SQLite, the easiest will probably be to just use SQLite, plus you get the benefits of a relational database. You can refer to the Notepad tutorial for a start.
It depends on the data you have. If it is largely text I would store it in an android string resource file. If it is somehow structured and has IDs and relations store it in a database.
We had a University project requiring a local database on an android phone. What whe did was to use SQLite to write on a database stored on the memory stick. (We couldn't find the right permissions to write directly on the file system)
Check the API website for the android.database.sqlite package first.
And here for a nice tutorial :
http://www.devx.com/wireless/Article/40842

Categories

Resources