• 8 hours
  • Hard

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/27/23

Identify the different ways data is stored on Android devices

How do I store data in Android?

In this course dedicated to Android storage, you’ll study different ways to save (or persist) content in user's phones, 100% offline, without any need for the internet! Before we can get into the nitty-gritty of how to do this, let's review the different ways Android has to save app data on phones. 

Storing data with shared preferences

In the first course of the Android curriculum, we covered SharedPreferences, which is a way of storing primitive, simple data. Basically, you store only variables (values), which you then retrieve using a single identifier (key).

This type of storage is called key-value storage. The variables (and their corresponding keys) will automatically be stored in an XML file directly on the user’s telephone. That file may be defined in private mode to allow only your app to access its content, or public if you want other apps to be able to access it.
 

Storage using Shared Preferences
Storage using shared preferences

Storing data with databases

To store structured data, Android natively offers the ability to use an SQLite database. This type of storage will enable you to organize and persist large quantities of structured data directly on the user’s phone, which can be worked on using SQL language.  

We’ll be talking about using and implementing SQLite on Android (with Room) in greater detail in the second part of this course. 

Storage using a database
Storage using a database

Internal and external storage

To support file data storage (photos, videos, PDFs, etc.), Android devices have two storage spaces: internal and external.

These names come from the time when most Android devices had both a physical, non-volatile internal memory, and removable memory, such as an SD card. Today, removable memory is falling out of favor, and many devices now divide their physical storage space into separate partitions: internal and external memory.

So these two storage spaces still exist separately, right? Even if the device doesn’t have a port for removable memory like an SD card? 

Exactly!  😄 In fact, there are several differences between these two storage spaces:

 

INTERNAL STORAGE

EXTERNAL STORAGE

Availability

Files stored in this space will always be available.

Files stored in this space will not always be available. That’s because they can be removed by the user at any time.

Accessibility

Files stored in this space can only be accessed by your application.

Files stored in this space can be accessed by anyone. Therefore, you have no control over them.

Life span

When the user uninstalls your app, the files will automatically be deleted from this storage space.

When the user uninstalls your app, the files will not be deleted from this storage space (unless you explicitly defined them to).

A few words on the internal storage space 

By default, the files stored in the internal storage space will be private and can only be accessed by your app. This means they won't be visible to other apps or to the user (unless he or she has a phone with root access).  😊

The internal storage space can also be used if you want to temporarily save data in a special cache folder. So, if your user’s telephone is short on storage space (or if a cleaning program like Clean Master has gone through... 😋), the contents of this folder will automatically be erased to free up some space. 

A few words on the external storage space

The files stored in the external storage space are to be considered as public and accessible by everyone... :waw:

This is where you will find the Documents, Downloads, and Music public folderes. You have no control over what is stored in this space. You can simply define them as public (not be deleted when the user uninstalls your application) or private (deleted when the user uninstalls your application).

Internal and External StorageInternal and external storage 

Overview of the app SaveMyTrip

Overview of the app SaveMyTripOverview of the app SaveMyTrip

SaveMyTrip is the mini-app that we will develop throughout this course. Its objective is to be able to easily organize your future trips, thanks to its fantastic travel diary as well as its interactive list of things to do, all accessible 100% off-line! 😀

You can download this mini-app SaveMyTrip as a nearly blank state, save for a few files:

  • Resource files: The resource files colors.xml, dimen.xml, strings.xml and styles.xml file are already preconfigured.

  • Layout files: To streamline the code found in the chapters, the XML design of the app's three screens was already performed. Feel free to check them out to absorb their info.

  • Classes of activities: The BaseActivity class, as well as the activity classes MainActivity, TripBookActivity, and TodoListActivity have already been created.

  • External libraries: The Glide library is also installed via Gradle.

  • The link between xml and java: We use ViewBinding to recover our various widgets in the java code.

I have also chosen to organize the structure of the project by features, rather than MVC. But this doesn't change that fact that our architecture will still be MVC... 😉

The focus of this course is on how you store data in Android, rather than on creating a graphical user interface, which you should be proficient in by now. That's why we're not starting with a blank app. 😎

And that's all! Launch the SaveMyTrip application. The result should be as follows:

INSERT GRAPHIC

Once again, feel free to thoroughly examine the basic code already produced, so you can understand it a little. You will see, everything is simple and has already been seen in the previous courses...

Let's recap!

There are 3 ways to store data locally:

  • shared preferences to store simple primitive data;

  • databases to store structured data;

  • internal and external storage to store data in file form.

Now that you know the theoretical part about the different ways of storing data locally, we will see how to create a file on the external storage of the smartphone.

Example of certificate of achievement
Example of certificate of achievement