So I'm making a weather app and I found when the app is idle for too long and destroyed that the forecast data is lost. So I think the best solution is to save the data to internal storage and retrieve it when it's needed.
The class containing the data has 2 arrays of other data classes, and it all contains strings, ints and doubles.
What is the best way to save this data? The options I've seen are a preferences file or saving it to a file in local storage.
I didn't think the preferences file would work because it sounds like that is more meant for smaller amounts of data.
I've been trying to save to local storage, but that hasn't been going well. Not too familiar with manually saving bytes to a file, and the Android developer guides aren't very comprehensive at all. I tried to serialize it, but get the not serializeable error. As far as writing the bytes I think I got it correctly written by converting strings to bytes using getBytes, saving the ints as they are, and converting doubles to strings then using getBytes. I'm not sure if that is even the correct way to do it, and I'm not sure how to read the bytes because you have to read a specific number of them. Do I have to find out how many bytes each type of data is going to be? For example finding the type of string encoding and reading the number of bytes for that data type?
What would be the best way of going about this? Should I just use the preferences file? Can someone either tell me how or point me to a resource for reading data from a file like this? Any help would be appreciated.
I tend to use preferences for different amounts of data. Shared preferences save to an xml file. Here is an example:
SharedPreferences.Editor editor = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE).edit();
editor.putString("name", value);
... // add all your other items here to save
editor.commit();
To get the data just do something like this:
SharedPreferences preference = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
preference.getString("name", defaultValue);
It is pretty simple and easy to use. I do sometimes save large data sets. For those, I will usually convert the data to json and then save that json as a string. When I get the data I will convert from json to java.
Another option is to use a sqlite database and store all your data there. There are many tutorials and documentation on how to save to a database.
Here is a great read to get you started: https://developer.android.com/guide/topics/data/data-storage.html
Related
I download big json files 8x(2mb) and used Gson to convert them into java objects. Now I need to make these objects available to all the activities. is it safe to save them as static variables?
You must be very lucky to avoid the out of the memory exception.
I would probably store the object to Room database(or any sort of SQL) while parsing and read when required.
Or just store the JSON as a binary file and read again necessary bits when its required since I don't know the usage of the JSON data I can't comment on more... But definitely avoid storing in the static variables.
I want to store multiple values (String, Int and Date) in a file via Java in Android Studio.
I don't have that much experience in that area, so I tried to google a bit, but I didn't get the solution, which I've been looking for. So, maybe you can recommend me something?
What I've tried so far:
Android offers a SharedPreferences feature, which allows a user to save a primitive value for a key. But I have multiple values for a key, so that won't work for me.
Another option is saving data on an external storage medium as file. As far as good. But I want to keep the filesize at minimum and load the file as fast as possible. That's the place, where I can't get ahead. If I directly save all values as simple text, I would need to parse the .txt file per hand to load the data which will take time for multiple entries.
Is there a possibility to save multiple entries with multiple values for a particular key in an efficient way?
No need to reinvent a bicycle. Most probably the best option for your case is using the databases. Look into Sqlite or Realm.
You don’t divulge enough details about your data structure or volume, so it is difficult to give a specific solution.
Generally speaking, you have these three choices.
Serialize a collection
I have multiple values for a key
You could use a Map with a List or Set as its value. This has been discussed countless times on Stack Overflow.
Then use Serialization to write and read to storage.
Text file
Write a text file.
Use Tab-delimited or CSV format if appropriate. I suggest using the Apache Commons CSV library for that.
Database
If you have much data, or concurrency issues with multiple threads, use a database such as the H2 Database Engine.
So I am working on my app, and it parses data from the LoL (League of Legends) API, so far so good, but the only problem is that if the user closes the app, and reopens it parses the data again. I want the objects I parsed from the JSON data to be saved, once it initially parses them, instead of redownloading them every time I reopen the app. It slows my app down.
There are a few ways to do that, but they all basically doing the same thing: serialize/de-serialize objects. The difference will be mostly what kind of persistence storage and what serialization mechanism are you using.
As for persistence you can use for example file, data base, etc.
Java provides human-unreadable binary serialization. Instead you can use JSON, XML, etc. formats. I assume Java built-in serialization will be the fastest, but it won't be deserialize if your class changed in the mean time.
Local persistence also would allow to map objects you parsed into database and load them pieces by pieces as needed.
Save your JSON data as a string in Android SharedPreferences. It is the most convenient and fastest method for you.
Using SharedPreferences
I'm developing a little game for android and i'm at the point where i want to save data of the progress the user has made so far.
I read about sharedpreferences, SQLlite... i have used some XML parsers aswell but my question is, if i want this:
Fast performance.
Secure or encryptable.
I want to point out that the data to save is not very long, profile name, levels completed, faction selected...
Thanks in advance!
Any solution will be "fast enough" for loading/saving the data... and any of them will be encryptable (as in they can all store string data).
SharedPreferences would probably be the simplest way to go. Perhaps store your data as JSON, then store the entire JSON string in SharedPreferences. Then you can easily add encryption to it later.
I'm building a home replacement app. I need to store the ArrayList with the apps the user picked to show on the launcher in the internal memory. I mean the array mustn't be deleted when the app is closed.
I'm very close to finishing the app and I don't think I'll work a lot more on Java, I'm not a programmer so I just want the easiest way to do it. How can I store and retrieve an ArrayList in the internal memory?
Use SharedPreferenes to store and retrieve the arrayList..
Check this link..
Save ArrayList to SharedPreferences
You should store such data as Arraylists in the database and when you relaunch the app just fetch the data from database(SQLite) and display it to the user.
Heres a good tutorial on android SQLite.
Take a look at this link, for all the different data storage mechanisms in android. But for your requirement I would suggest using a db.
The easiest way is to use Java serialsation. It writes the content of an object to a (file-) outputstream. Thats a few lines of code.
a bit better (faster, less bytes, readable by other non java systems) is to uses a custom serialisation, using DataOuputStream. That creates a binary file in the format you define.