Course Content

Course Content

Description

Data Storage in Android

Android offers a few structured ways to Data Storage. These include -

  1. Shared Preferences
  2. Internal Storage
  3. External Storage
  4. SQLite Storage
  5. Storage via Network Connection (on cloud)

 

1. Shared Preference in Android

Shared Preference is used to store our data even when we close the application.

Shared preferences allows activities and applications to keep preferences, in the form of key-value pairs.

Similar to a Map that will persist even when the user closes the application.

Android stores shared preferences setting as XML file in shared prefs folder under DATA/data/{application package} directory.

Shared preference is application specific, that is the data is lost on performing one of the following option -

  1. On uninstalling the application.
  2. On clearing the application data (through Settings)

 

Access to the Preferences

To get access to the preferences we have three APIs to choose from :

getPreferences()

getSharedPreferences()

getDefaultSharedPreferences()

 

  • getSharedPreferences() (String PREFS_NAME, int mode)

PREFS_NAME is the name of the file.

MODE is the operating mode.

 

There are different types of MODE which are readymade available like -

MODE_PRIVATE

MODE_WORLD_READABLE

MODE_WORLD_WRITEABLE

MODE_MULTI_PROCESS

MODE_APPEND

MODE_ENABLE_WRITE_AHEAD_LOGGING

 

Operating Modes Applicable

You will call the edit method of Shared Preference instance and will receive it in an editor object.

Its syntax is -

Editor editor = sharedpreferences.edit();

editor.putString(""key"",""value"");

editor.commit();

  • apply() is an abstract method. It will commit your changes back from editor to the sharedpreference object you are calling.
  • clear() will remove all values from the editor.
  • remove(String key) will remove the value whose key has been passed as a parameter.
  • putLong(String key, long value) will save a long value in a preference editor.
  • putInt(String key, int value) will save a integer value in a preference editor.
  • putFloat(String key, float value) will save a float value in a preference editor.

 

2. Android Internal Storage

Reading and Writing Text File in Android Internal Storage

Android offers openfileinput and openfileoutput from the Java I/O classes to modify reading and writing streams from and to local files.

 

openFileOutput()

It’s syntax is given below :

FileOutputStream fOut=openFileOutput(“file name”,Context.MODE_PRIVATE);

After that we can call write method to write data on the file.

It’s syntax is given below :

                String str=”test data”;

                fOut.write(str.getBytes());

                fOut.close();

 

openFileInput()

It’s syntax is given below :

FileInputStream fin=openFileInput(file);

Syntax :

int c;

                String temp=””;

                While((c=fin.read())!=1)

{              temp=temp+Character.toString((char)c);

                }

 

3. Android External Storage

External storage such as SD card can also store application data, there is no security enforced upon files you save to the external storage. In general there are two types of external storage :

 

Primary External Storage : In built shared storage which is ""accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer""

 

Secondary External Storage : Removable storage, example SD card.

 

4. Android SQLite Database

SQLite is a very light weight database which comes with Android OS. It combines a clean SQL interface with a very small memory footprint and decent speed.

For Android, SQLite is ""baked into"" the android runtime, so every Android application can create its own SQLite databases.

Its native API is not JDBC, as JDBC might be too much overhead for a memory-limited smartphone.

Once the database is created successful its located in data/data//databases/ accessible from Android device monitor.

SQLite is a typical relational database, containing tables (which consist of rows and columns), indexes etc.

 

SQLiteOpenHelper is designed to get rid of two very common problems :

1) When the application runs the first time - At this point, we do not yet have a database, and so we need to create the tables, indexes, starter data, and so on.

2) When the application is upgraded to a newer schema - Our database will still be on the old schema from the older edition of the app. We will use an option to alter the database schema to match the needs of the rest of the app.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere