• 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 6/12/19

Create a file in internal storage

Log in or subscribe for free to enjoy all this course has to offer!

Now that you know how to save content to external storage properly, you will see how to let users save text to their phone's internal storage, to protect it even more. 🙂

We will still need to ask the user’s permission to use their internal storage, right?

Actually, we don't! When you save information on the internal storage of your user's phone, you don't need their permission since this space is allocated to your app. 😉 So in this chapter, we will allow users to save their text (and the file tripBook.txt) in the internal memory of their phone:

  • Either in the app's internal storage.

  • Or, in the dedicated cache of the app's internal storage.

Updating the activity

Now let's get serious! Since we created a rather general class utility in the previous chapter, StorageUtils, we’ll be reusing it here (without editing it, obviously!). Let’s update our activity to call the appropriate methods when the user clicks on the radio buttons dedicated to storage in internal memory:

Excerpt from TripBookActivity.kt

class TripBookActivity : BaseActivity() {
...
// --------------------
// ACTIONS
// --------------------
...
private fun save() {
if (radioButtonExternal.isChecked) {
writeExternalStorage() //Save to external storage
} else {
// 3 - Save on internal storage
writeInternalStorage() //Save to internal storage
}
}
// ----------------------------------
// UTILS - STORAGE
// ----------------------------------
@AfterPermissionGranted(RC_STORAGE_WRITE_PERMS)
private fun readFromStorage() {
...
if (radioButtonExternal.isChecked) {
...
} else {
// 2 - Read from internal storage
if (radioButtonInternalVolatile.isChecked) {
// Cache
editText.setText(StorageUtils.getTextFromStorage(cacheDir, this, FILENAME, FOLDERNAME))
} else {
// Normal
editText.setText(StorageUtils.getTextFromStorage(filesDir, this, FILENAME, FOLDERNAME))
}
}
}
...
// 1 - Write internal storage
private fun writeInternalStorage() {
if (radioButtonInternalVolatile.isChecked) {
StorageUtils.setTextInStorage(cacheDir,
this,
FILENAME,
FOLDERNAME,
editText.text.toString())
} else {
StorageUtils.setTextInStorage(filesDir,
this,
FILENAME,
FOLDERNAME,
editText.text.toString())
}
}
}

For the explanation, we're starting at the bottom of the code we just wrote, and working our way up.  We added a method (1) named writeInternalStorage dedicated to writing in our file in internal storage. This will be called by the  save method (3) when the user clicks on the Save button on the toolbar. We've also edited (2) the method  readFromStorage  to read the file from internal storage whenever the activity is run, or when the user clicks on the radio buttons.

And what’s more, all this should remind you of something!  😉 Yes, we used the same approach to write and read to our external storage in the previous chapter by calling the methods getTextFromStorage  and  setTextInStorage, changing only the parameter corresponding to the root directory to match that of the internal storage:

  • filesDir: This property contains the path (file) to the root of the internal storage space for your application.

  • cacheDir: This property contains the path (file) to the root of the cache of your app's internal storage space.

Saving to internal storage
Saving to internal storage

Now run the app and try saving text in the internal storage space! 🙂

Let's recap!

In this chapter, we focused on internal storage, namely updating our activity so that when users click on the options for storing information in internal memory, the activity calls the methods we need. Since this chapter is shorter, take the time to read and reread your code, to understand the logic behind it. And don't forget the golden rule of any good developer: practice makes perfect! Good luck. 😇

Example of certificate of achievement
Example of certificate of achievement