Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Exploring local data storage options

From: Android SDK: Local Data Storage

Video: Exploring local data storage options

The Android SDK includes many options for storing data locally with an Android app. All of these options give you the ability to persist information between application launches or between device sessions--that is after a device is shutdown and then restarted--because in all of the options that I'm going to describe, you'll be saving files to local flash memory or other persistent media. Here are the three major options for local data storage: shared preferences, arbitrary files that you simply create and save to disk, or SQLite relational databases.

Exploring local data storage options

The Android SDK includes many options for storing data locally with an Android app. All of these options give you the ability to persist information between application launches or between device sessions--that is after a device is shutdown and then restarted--because in all of the options that I'm going to describe, you'll be saving files to local flash memory or other persistent media. Here are the three major options for local data storage: shared preferences, arbitrary files that you simply create and save to disk, or SQLite relational databases.

Here is a little bit more detail about each of the options. A shared preference is simply a key-value pair, a key, which is a string, and then a value, which can be one of many data types. It's up to you as the Android application developer or designer to decide what preferences are needed for your application. Typically, a preference is used either to present specific information to the user or to determine the behavior or appearance of an application.

You can manage your preferences in one of two ways, either programmatically with Java using a special API that's a part of the Android SDK, or through a preference activity, a specific kind of user interface that's defined by the SDK and will manage your preferences for you. Regardless of whether you do it programmatically or through an activity, preferences are stored unencrypted. They're stored in a simple XML file on your persistent data storage area, and the creation and then reading of the XML content is handled completely by the underlying API.

As the developer, you'll create your preferences either programmatically or through an activity, and then you'll be able to read them programmatically with Java. But the mechanics of the XML file are handled for you. Shared preferences are great for very simple values, but when you need to store more complex information, you might want to go to the next option: creating your own files. The Android SDK has a complete system for creating, storing, and reading files, and you can create and read these files using the persistent media that's a part of the Android device.

There aren't any special file types. You can create pretty much anything, including binary files like images, XML, JSON, or delimited data files, or anything else for which you can program. You'll be able to choose from a variety of approaches to creating and reading these files, including APIs that are unique to Android or other APIs that are available in all implementations of Java. You can control access to these files by where you place them in your storage.

You can designate a file to be internal, which means it can only be accessed by the application that created it, or external, which means that it's available to other applications as well. Finally, if you need the power of a complete relational database, you can create an SQLite file. An SQLite database is a complete relational database that can store multiple database tables and has statically typed columns, so you can designate a data type as being a string, an integer, a double, and so on.

SQLite is a very powerful little database, and it's used on a lot of different platforms, not just Android. You can find complete information about SQLite from the homepage: www.sqlite.org. But the most important information you'll need will be in the documentation for this package: android.database.sqlite. Android's implementation of SQLite is pretty plain vanilla, but what's important is the programming interface that's provided.

It lets you standardize how SQLite files are created and where they're placed on disk. When you create an SQLite database file programmatically, the database will be stored locally and it will be associated with the application, so later on, when the application is uninstalled, the associated database file will be deleted with it. If you want to share this sort of complex data with other applications on a device, consider creating a custom content provider. And I'll show you how to do that toward the end of this course.

All Android devices, regardless of whether they are cell phones or tablets, are networked, and so the final way that you can deal with data is through the network. You can store application data on a networked server and then manage your network communications using a variety of APIs. You can use java.net, which is a part of the core Java language, or android.net, a set of classes that are unique to Android. And if you're programming for Honeycomb, Android 3 or later, you can use a class called Loader, that greatly simplifies the loading of data from a networked server.

Data exchange between an Android device and a networked data source is typically handled using what are called restful web services: simple XML files that are requested and retrieved from a networked server and then pushed back up to the server to make requests. If you're familiar with SOAP-based web services, you won't be able to use those so easily in Android because there's no built-in support for them. This course, however, focuses on local data storage, and so the use of networked data is outside the scope of the course.

But let's get started with local data storage with Android.

Show transcript

This video is part of

Image for Android SDK: Local Data Storage
Android SDK: Local Data Storage

29 video lessons · 5888 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 6m 34s
    1. Welcome
      50s
    2. What you should know before starting this course
      2m 11s
    3. Using the exercise files
      3m 33s
  2. 21m 6s
    1. Exploring local data storage options
      5m 58s
    2. Configuring Eclipse and the Android Developer Tools
      5m 29s
    3. Creating an Android virtual device
      3m 23s
    4. Creating a new Android project
      6m 16s
  3. 31m 30s
    1. Using preferences in Android apps
      5m 28s
    2. Defining preferences with Java
      9m 31s
    3. Defining shared preferences with an activity
      10m 50s
    4. Listening for changes to shared preferences
      5m 41s
  4. 51m 13s
    1. Creating and reading files in internal storage
      10m 18s
    2. Creating and reading JSON data files
      9m 48s
    3. Working with files in external storage
      11m 22s
    4. Parsing a read-only XML file with XmlPullParser
      11m 46s
    5. Parsing a read-only XML file with JDOM
      7m 59s
  5. 1h 8m
    1. Creating a new SQLite database
      2m 52s
    2. Defining a database with SQLiteOpenHelper
      12m 12s
    3. Managing the database with a DataSource class
      9m 38s
    4. Inserting data into a database table
      10m 29s
    5. Retrieving and displaying data
      11m 44s
    6. Importing data from XML to SQLite
      5m 15s
    7. Filtering and sorting data
      9m 27s
    8. Accessing a database from the command line
      6m 46s
  6. 42m 0s
    1. Improving the data display
      9m 29s
    2. Passing user-selected data to a detail activity
      11m 36s
    3. Working with multiple database tables
      10m 28s
    4. Deleting data from database tables
      10m 27s
  7. 49s
    1. Where to go from here
      49s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Android SDK: Local Data Storage.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.