Last Updated:

Android Internal Storage: An example of working with internal memory

Internal memory of Android. Theory

Any Android device allows you to store personal data in the internal memory. If one application saves or downloads a file to internal storage, other applications will not be able to access those files. These files can only be managed by this application. When a user deletes it, all the internal files of that application are also deleted.

However, note that some users may be able to root access their Android devices. In this case, users will be able to access any files.

Create and read files from Android's internal memory

Android uses Java IO methods and classes to work with local files.openFileInput()openFileOutput()

Note that these methods do not take as input the path to the file (for example, path/to/fileName.txt), but simply get the filenames "fileName.txt".

The method is used to create and save the file. An example of use is shown below:openFileOutput()

The method returns an instance of the . After that, we can write the data to the file. Example:openFileOutput()FileOutputStream

The method is used to read data from a file. It returns an instance of . Example of use:openFileInput()FileInputStream

Next, we read the data from the file into a string. For example:

An example of working with internal memory (Android Internal Storage). Practice

Below is the structure of our simple Android project:

The layout code contains buttons for both writing data to the file and reading. Note that we defined the method in the layout, not in the code (you can assign a listener in the code yourself - it doesn't change anything. It's just that we're teaching Android, so we're trying different ways to control the behavior of widgets):activity_main.xmlwriteBtnreadBtnOnClick

To assign a method in a layout, we have to define the method in the code with the parameter , otherwise Android will throw an error that the method has an incorrect signature (there is no method parameters).OnClick()ViewView

The class contains an implementation of methods for reading and writing files: MainActivity

The result of working with internal memory (Android Internal Storage):

android internal storage

We entered the text in the field, clicked the 'Write to File' button and received a message about the successful recording. Now delete the text from the input field and click 'Read from file' – you will receive a message about the successful reading from the file.

Curious: Where is the simplefile.txt file stored?

The simplefile file.txt is stored in your smartphone at . In our case, . This file can be accessed through File Explorer.

data --> data --> [package name] --> files --> simplefile.txt[package name] = com.p-qc.hellointernalstorage

Follow the updates of the Android Development section and subscribe to new articles!