Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Android Data Storage Technique to use?

The android documentation has the following options below but does not explain what circumstances each is best for. What are the pros and cons of each method? e.g. Under what conditions would SQL be better than Shared Preferences?

  • Shared Preferences
  • Internal Storage
  • External Storage
  • SQLite Databases
  • Network Connection
like image 578
Declan McKenna Avatar asked Apr 03 '12 03:04

Declan McKenna


People also ask

What is the best way to store data in Android?

Shared Preferences is the easiest to use, especially if you want to store discrete primitive data types. However, internal and external storage is best for storing files such as music, videos, and documents, while SQLite wins if you need to perform fast searches and queries on your data.

Which database is used in Android for data storage?

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.

What are the different storage method in Android?

Android provides many kinds of storage for applications to store their data. These storage places are shared preferences, internal and external storage, SQLite storage, and storage via network connection.

What is the best method for storing data securely and privately in a mobile app?

More user-friendly and recommended way is to use the Android KeyStore API system (itself or through KeyChain) to store key material. If it is possible, hardware-backed storage should be used. Otherwise, it should fallback to software implementation of Android Keystore.


2 Answers

Different Storage options in Android

enter image description here


Content Providers

enter image description here

  • Consider the structured data added to the device from application1 is not accessible to another application2 present in the same device but the profile photo added to the device by application1 is available to the application2 running in the same device

  • Consider android device as a city, the applications in it are the houses in the city, people in the houses(application) are the data. Now content provider is like an broker in the city(android device). This broker provide access for the people in the city for finding different houses referring as the content provider in the android device provide access for the data in the device for different applications.


Shared Preferences

enter image description here

  • Consider I have an App say a Face book App which I use to log in to my account.

  • Now the very first time I enter my username and password to get access to my account. Say I log out of the application an hour later again I use the same Face book App to login again to my application.

  • I have to enter username and password again to login to my account and I set a theme to my application and other settings on how my app looks in my current phone

  • This is un-necessary because consider I am using my phone to login to the application. So I will always use my phone to login again and again, thus entering my credentials again and again is more work shows it’s not a user friendly app

  • Shared Preferences is very handy in such scenarios where I can use its feature to share my data in a xml file Which physically exists in the Android app installed in my phone which is not destroyed even if the app is closed. Here we can save user preferences data of the current application.

  • As a result next time I open my app in my phone I can see the data automatically filled in the necessary fields and the settings are


File Storage

enter image description here

  • In Android we can use the device storage space to store the data in it for the applications. The type of data involves things such as a text file, image file, video file, audio file etc.

  • As seen in the figure as we can see that there are two places we can do this. One way is to write the raw files into primary /secondary storage. Another way is to write the cache files into the primary/secondary storage.

  • There is also difference between storing raw data and the cache data, the raw data once stored in memory by user has to be explicitly deleted by the user explicitly otherwise it would exist till then. Cache data stored in memory is not a permanent data because the system automatically deletes it if it feels there is shortage of memory.

enter image description here

Internal Storage:

  • Consider a user in an application has stored data in internal storage, then only that user of that application has access to that data on the mobile and that data is automatically deleted when the user uninstalls the application. Speaking of which internal memory is private.

  • The apps internal storage directory is stored using the name package name in a special place in the android file system.

  • Other apps or users of current app have no access to the file set by a particular user and a particular app unless it is explicitly made available to the user for readable/writable access.

enter image description here


SQLite

enter image description here

  • Sqlite is used to store more structured data locally in a mobile where the android app is running. Structured data involves as of which shown in the figure like a student’s information in the form of rows and columns.

  • Sqlite offers similar functionality like Mysql and oracle but with limited functional features. Some of the things involve performing query operations on tables. There are features though like creating views but also some features are not available like stored procedure.

  • Sqlite is very helpful in storing complex and large data which can be downloaded once and can be used again and again until the application is running. When the application is closed the sqlite database is also destroyed.


Putting all the pieces together

enter image description here

like image 108
Devrath Avatar answered Oct 11 '22 13:10

Devrath


  • Shared preferences are good for storing ... an application's preferences, and other small bits of data. It's a just really simple persistent string key store for a few data types: boolean, float, int, long and string. So for instance if my app had a login, I might consider storing the session key as string within SharedPreferences.
  • Internal storage is good for storing application data that the user doesn't need access to, because the user cannot easily access internal storage. Possibly good for caching, logs, other things. Anything that only the app intends to Create Read Update or Delete.
  • External storage. Great for the opposite of what I just said. The dropbox app probably uses external storage to store the user's dropbox folder, so that the user has easy access to these files outside the dropbox application, for instance, using the file manager.

  • SQLite databases are great whenever you are going to use a lot of structured data and a relatively rigid schema for managing it. Put in layman's terms, SQLite is like MySQL or PostgreSQL except instead of the database acting as a server daemon which then takes queries from the CGI scripts like php, it is simply stored in a .db file, and accessed and queried through a simple library within the application. While SQLite cannot scale nearly as big as the dedicated databases, it is very quick and convenient for smaller applications, like Android apps. I would use an SQLite db if I were making an app for aggregating and downloading recipes, since that kind of data is relatively structured and a database would allow for it to scale well. Databases are nice because writing all of your data to a file, then parsing it back in your own proprietary format it no fun. Then again, storing data in XML or JSON wouldn't be so bad.

  • Network connection refers to storing data on the cloud. HTTP or FTP file and content transfers through the java.net.* packages makes this happen.

like image 35
Teddy Avatar answered Oct 11 '22 13:10

Teddy