• 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

Create a file in internal storage

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())
        }
    }
}

Explanation: 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:

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

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

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

Saving to internal storage
Saving to internal storage

Let's recap!

  • It is not mandatory to ask the user for permission to use the internal storage.

  • You can store either in a cache or directly in the internal storage.

  • We reuse the StorageUtils class which is generic for internal and external storage.

As we have just seen, storing files in the internal space is not rocket science! But sometimes it is useful to be able to share these files. That's what I'm going to show you in the next chapter!

Example of certificate of achievement
Example of certificate of achievement