I was wondering if there is a way to put data in Sharedpreferences on Android without creating a String. I have a sensitive char array(base64 encoding) that I would like to store in an encrypted Sharepreference but I would like to avoid creating a String(using putString()) in the process given that they are immutable. Is there a way to accomplish this?
Thanks!
Well for this scenario I can suggest you can hide actual content with key-value pattern, just create any significant key for your data at point of entry and set data as value for same and save both in SQL-lite or any supported database, and then retrieve same values using key at your destination.
By this way only key will be shared in internal transaction and data will never reveal on any mid-points.
Hope this will help.
Related
I have a HashMap<Integer, ArrayList<MyTeam>> where the class MyTeam is a POJO. There will be at most 5 items in each ArrayList object. Is it possible to save this kind of HashMap to SharedPreferences? If not, what is another alternative? I need this data to be saved when the app is closed and reloaded when it starts up.
I've looked at this answer but the Key and Value attributes are both String and my case is a bit more complicated than just String data types. Will this method still work? Is there a better way?
Is it possible to save this kind of HashMap to SharedPreferences?
Not directly. You are welcome to convert that into JSON and save it as a string preference. Then, to load it back in, you would read in the string and use a JSON parser to reconstitute your objects.
This class demonstrates the basic technique, though it is saving a List<Uri>, not a HashMap<Integer, ArrayList<MyTeam>>. I specifically used this sample to demonstrate the use of the built-in JSONReader and JSONWriter classes; for a real app, I would consider Gson or a similar JSON parsing library.
If not, what is another alternative?
Save it to a SQLite database, probably through a couple of tables. Or, save it to some sort of file, in a file format of your choosing (e.g., JSON, XML).
When i write anything into shared preferences it overwrites the previous value, how can i continuously inject values in preferences and fetch it out all at once like an array? I do not want to use database for this operation.
Thanks
You can use JSON as #CommonsWare points out to store your data in SharedPreferences. I have such an answer that using GSON here:
https://stackoverflow.com/a/5968562/617044
Though not ideal, if you want to use Preferences only, You can store String in preferences. When next value is to be added, retrive the old one add 'comma' and new value.
Whenever you want to use these values, use StringTokeniser. Tokenise elements by 'comma' character.
using Set in sharedpreference is API lv11;
i have a project need to parse to many types of nested items
and need to save it to sharedpreference using only string,
the xml items is very complicated to save as a normal string to
sharedpreference, if i use normal string it need to create
so many sharedpreference names and values,
My question is, is JSON is the alternative because its string is
like a list so that i can read easily the items in per category.
You may want to consider using internal storage over SharedPreferences.
Here is more information on internal storage.
Yes you can use JSON as a string and save it to the SharedPreference that is because JSON is faster , smaller and less verbose structure than XML.
can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.
I have an app that searches in a database. I want to store last 10 search values (and I want them to remain even if the user closes the app) to use them as an adapter in AutoCompleteTextView.
SharedPreferences doesn't seem to support arrays or arraylists. What's the best approach here?
You can use a table in a SQLiteDatabase to store the search history, and use the standard SQL API to access it.
Or you can use a file in XML, JSON, YAML, CSV, plain text, or whatever you like to persist the history. The advantage is simplicity and (maybe) performance. The disadvantage is that you'll have to serialize and deserialize yourself (a possible variation is to serialized a Java object directly)
SharedPreferences support boolean int, float, long and String.
BUT ArrayList are Serializable, so if you also declare you object Serializable, you can encode them into a ByteBuffer with an ObjectOutputStream, then convert this byteBuffer into a String, and finally store it into a SharedPreferences, or better to a binary file (as bytebuffer).
In your case, where you just have to save String, it is easer run through every element of the array and save them as "arrayName"+index, and finally save the size of the array.