Chapter 5. Storing and retrieving data

 

This chapter covers

  • Storing and retrieving data with SharedPreferences
  • Using the filesystem
  • Working with a SQLite database
  • Accessing and building a ContentProvider

Android provides several ways to store and share data, including access to the file-system, a local relational database through SQLite, and a preferences system that allows you to store simple key/value pairs within applications. In this chapter, we’ll start with preferences and you’ll create a small sample application to exercise those concepts. From there, you’ll create another sample application to examine using the filesystem to store data, both internal to our application and external using the platform’s Secure Digital (SD) card support. You’ll also see how to create and access a database.

Beyond the basics, Android also allows applications to share data through a clever URI-based approach called a ContentProvider. This technique combines several other Android concepts, such as the URI-based style of intents and the Cursor result set seen in SQLite, to make data accessible across different applications. To demonstrate how this works, you’ll create another small sample application that uses built-in providers, then we’ll walk through the steps required to create your own ContentProvider.

We’ll begin with preferences, the simplest form of data storage and retrieval Android provides.

5.1. Using preferences

5.2. Using the filesystem

5.3. Persisting data to a database

5.4. Working with ContentProvider classes

5.5. Summary

sitemap