Chapter 7. Storing data locally

 

In this chapter

  • Reading and writing files
  • Setting and remembering shared preferences
  • Working with SQLite databases

Data is a precious thing and will last longer than the systems themselves.

Tim Berners-Lee

Data is essential to any application, and Android provides several local storage avenues. But Android doesn’t stop there—you also have access to data from other applications on the device and the network, which we’ll learn about in upcoming chapters. First, we’ll focus on local data storage.

To explore local data storage we’ll start with the filesystem. Don’t forget: Android devices are small computers, and they have filesystems. We’ll see how you can check whether the filesystem is available, how you can use it, how permissions come into play, and the differences between internal and external storage. After basic files, we’ll visit SharedPreferences, which is a helpful class for storing key-value pair data. SharedPreferences uses the filesystem, but it hides some of the details and makes for a more convenient approach in some cases. Once we understand files, we’ll look at more sophisticated data storage using Android’s built-in database, SQLite. SQLite isn’t the same as your typical server side relational database, but it’s no slouch either. We’ll explore how to use it, how to create a data access layer around it, and how it differs from what you may be used to.

7.1. Reading and writing files

7.2. Maintaining preferences

7.3. Working with a database

7.4. Inspecting SQLite databases

7.5. Summary

sitemap