Approaches to storing data in Android - android

I have started learning Android development and I have a very newbie question. I understand that Android can store data in SQLite for example, but what other approaches are there to the storage of data within your application?
Do Android apps ever have data 'embedded' within the application, in which case what sort of data structure or concept would this use?
I am thinking of a scenario where the data is static but is perhaps not a large enough dataset to warrant a database..e.g. an app with general knowledge questions and answers
Any guidance much appreciated
Rowan

Yes You are correct You can use SqLite Database for storage
other ways to store data is SharedPreferences
But in your case you wanrted to save questions and answers which is static one so you can create a text file and put that in your assets folder and you can read that file as any other text file in java
Refer this link how to read file from assets folder
1.Sqlite Database
2.Shared preferences
3.Internal memory
4.external memory i.e sd card

i would suggest you to go with Database. as it will let you store as much data as your app needed, There are some other option also present like
Sharedpreference i.e. cookies in general term. It let u store only few KB data and not good to store much data. When u retrieve data from cookies. All data will be store into ram and use app memory. that is use less when u do not need all data to retrieve and store into ram and then remove
Store into file and ship that file with your app. Yeah. this could be better idea again. you need to read it byte by byte. and hence reading to mid or last line will store all data into ram and hence will take memory.
Use Web Service to download data. It will let you store Large data and you have to download using Web APi. Hence it could be better idea. But this requires active Internet connection to play game to run app.
There must be some other option also present. You can search. surely you will find them :)
Overall Database it good solution for all app. As it will let you do search store delete and let you do other operation in less amount of memory. In Mobile Development Memory is very Important thing we have to take care of.
Let me know if you have other unclear thought.
item

You could also store info in a server.
Pros :
You can change the content without needing user-side update of the app.
Cons :
Your app (mostly the UI) would need to manage connection problems.
You may need to implement async tasks for querying data from server.

Related

approach to reload Complete state of application?

i have a question which i know its answers vary from one app to another.
consider an app like Telegram which you have a noticeably amount of data there like your contacts and chats. when you close the app and later you relaunch that, even if you don't have any connection to internet, app loads your contacts and chats and this is done in fraction of second. i want to know how these works are done and implemented?
for example they store all the data in disk in a file(storing that amount of data on disk can take long time moreover they cache images too), or we save each part of app in different files and we load them whenever user opens them.
You have two commonly used ways to store data in Android. SharedPreferences and local databases.
Before I continue, you should check out other articles regarding storing data on android. There are loads of them explaining it better and in-depth than what I can, I will just give some examples what you can use.
Start here or just google it!
SharedPreferences
SharedPreferences works in a Key -> Value, used for saving basic types of data. Good for storing user tokens, settings and similar things.
See more
Databases
Android uses SQLite for local data storage, and there are many libraries that can help you with this.
More notably Room, but there are other ones as well that are very popular to use. Databases are good for storing large amounts of data, like conversations and contacts in the example you are talking about.
Caching
You also have access to caching data, but keep in mind that it's meant as a temporary storage, and if you want to store something like user information or other complex data, you should stick to a database. Caching is useful for images.
This is called api data caching. It is important that we should know which data to be cached and which data should not be cached.
There is a library from NYtimes you can use it for api level caching with retrofit.

Caching in Android

I'm currently developing an android app in which I'm dealing with php files that returns json, the json then gets parsed and is used to fill up an arraylist of different objects.
What I'm trying to do is, I want to cache these objects in case of no internet so I can reload them and make the application useable offline.
Currently I have 2 solutions for this:
A) Make an SQLite database and creare tables with the same structure as the objects I'm trying to cache and then reload them on startup
B) Save the JSON strings inside the shared prefs and parse them on startup.
I didn't really find any best practices or tutorials when I came across data caching so I'm lost now and I have no idea what to do. So if you guys can please help out I'd be thankful.
You might find the following talk interesting:
https://www.youtube.com/watch?v=xHXn3Kg2IQE
As you can see, a lot of scaffolding goes into the ContentProvider approach. For a very simple app, it can be acceptable to just cache some JSON in a SharedPreferences. However, as your app becomes more complex, the advantages of the ContentProvider will be worth it.
You can use files instead of sqlite and shared preferences.
Shared prefrences is using for small data.
Sqlite is using when you need to make queries on stored data.
So I think it's better to cache in files, or if you are using something like retrofit or any network lib you can check if they support caching and use it.
Definitely not option B. Shares preferences are not suitable for storing large amounts of data. It's XML so cannot be queried like Sqlite. The time to retrieve a single item will increase linearly with the amount of data stored. Secondly JSON will have special character these will need to be escaped which means the storage size will increase even more.
There is however an option C. Using cache files. This approach and other store options available to you are described in the google developer guide and this is essential reading.
So in summary: you options are to parse the JSON and store it in sqlite or to save the json as a file in the cache directory.

Data Persistence Android

I'm developing a social photo app that is like instagram and I need to improve the application performance, to do that I need to persist some data, like timeline feed, photos, messages, etc.
I'm already using Shared Preferences to save user informations.
I don't know which is the best way to persist the data, I'm between SQLite or Files, to save the json data or something else.
Which is faster and gives me less trouble?
If somebody could help me I would appreciate it.
take a look at this caching images will provide better solution.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
If you need different queries on this data, than i strongly recommend to use SQLite. Cause it's not possible to make select queries on files without storing it to memory.
Even if you use this object as is without any special queries, you can store json objects to base.
SQL:
1) Fast.
2) Possibility to make select queries with different WHERE condition.
3) Easy update.
4) etc
I do not see the advantages to store this data to files...
If you will store data in files, it will slow if there will be a lot of data. Even if you will use sax parser.
But, images you should store as files, and only update link on it in data base.

Best Possible way to Store Data (Lot of Strings with Images) in android application

I am trying to build a application which will be a Ebook kind of (Lot of theory & diagrams) will be there.
Now what i want to know is that since there are many ways of storing the data which one will be the best
Storing in Database
XML
Or simple text files
I am very concerned about the security of the data as well. Since this will be a paid app, i want the data to be secured and also be fast and convenient.
Also, I thought of converting the doc files (Data) in to epub format & then use epub api's to access the data and show it on the android app screen, will this be a gud idea to go for? as compared to the above ways?
Which one will be more secure, fast, flexible & easy!
It depends on how you will access to this data. If you will store in xml you will must to read the whole file from the start to access to chapter (or load to memory, for example). It's not good idea if you will store big data.
Storing in SQL faster. You can gain access to any chapter. You don't need to read all data, like in xml.
Simple text file has the same problem like XML (xml is textfile).
The only one way to secure you data - encrypt it. If user will get root on their device, he will gain access to your files and databases. There is no meaning where you will store your data.
Depends on what is more important to you - speed or security.
Speed
Definitely SQLite, it isn't exactly the cleanest, but definitely the fastest way.
Security
Custom files which are encrypted - it will take a while to read the whole file and then decrypt it in order to display it, but you can be sure that the attacker will access the files encrypted and without the knowledge of the encryption - those data would be useless to him.
EPUB
If you're concerned about security then don't, unless you know how to apply DRM...and that is not a way to go honestly.
I think that the best way to store big amount of data is database. In Android it is sqlite database. I recommend you to put all your text data into sqlite database. You can structure it in easy and beautiful way. Then put your images into assets folder and store the pathes to the the images in database.
Advantages of database solution:
Always well structured data
Easy way to update data with version control system.
You can store and get fast accesses to really big amount of data.
You can use encryption to protect your data.
Disadvantages
It is more complicate to write good code for database solution then for XML or JSON one.
P.S If you will decide to use XML I recommend you to change it to JSON. It is faster and easier to use.
Which one will be more secure, fast, flexible & easy!
Secure: It mainly depends on encryption system.
Fast: SQLite, you can read some advantages of SQLite here Android Performance : Flat file vs SQLite
Flexible and easy: Storing the encrypted files in internal storage is a flexible and easy way. I think it is secure enough. Here you can get some android security tips about storing data http://developer.android.com/training/articles/security-tips.html#StoringData
for saving little data you can use xml for strings but you lose fast loading factor
sqlite is good for almost every purpose, but Security

Android Storing RSS feed parsed data in a database for offline use

I am parsing a Rss feed using sax parser in android. I am able to display the data online when net is connected but i also want it to work for offline use and update it when it gets connected again to internet / wifi. I have no clue how to go about it.
What should be my best approach now ? should i construct Sql database ? considering i have images as well.
Or there is any other simpler way. I would prefer simpler way.
I need some further suggestion on the Sql database approach here, First : My rss feeds gives image url links which i diplay using bitmap and insutstream at runtime but now for offline purpose i need to save complete images like whatsapp does right ? is this right ? if yes how to save complete images in database ? And last i want to save the complete database on sd card not in internal memory , storing data on sd card will work fine or it will create problem ? because whatsapp stores quite a data in internal memory !! if storing on sd card is not a problem how do i store complete data on sd card ?
This depends on how long you want the data to persist. Ask yourself:
Should this data be available to the users after rebooting the phone, or after force closing the app? Should it be available regardless of the last time I had connectivity, as up to date as possible given that?
In that case, then yes - you should use a database. Android has a number of built in helper classes for sqlite databases.
http://developer.android.com/training/basics/data-storage/databases.html
Which should get you started.
The images are pretty straight forward as you'll just stash a reference to the image(s) in the db. You would of course write these images to disk as well (on the sd card or some other place...) See:
Save bitmap to location
Your other options, afaik are:
1) SharedPreferences (not really suited to this).
2) Serializing your data and writing out/reading in from some file.
If you're still looking for more information on Database concepts and Android, here is a very good tutorial on the topic:
http://www.vogella.com/articles/AndroidSQLite/article.html
You can use droidQuery to download the RSS Feed and cache it. Working off of this gist, which expects you to use the android-rss library, you can add the following cache flags to your AjaxOptions object:
.cache(true).cacheTimeout(AjaxCache.TIMEOUT_NEVER)
This will make it so the response is cached until you explicitly call:
AjaxCache.sharedCache().clearCache();
Which you can do after the network is connected (for help on this, check out NetWatcher).
Note that using this cacheing mechanism allows a very simple solution that will only store as long as the app process is live. If you want to save across sessions - so that if the user opens the app later and is not online, you will want to use something more complex and long-lasting, such as SharedPreferences or SQLite. A good list of options can be found here.

Categories

Resources