Android: Storing Data and Accessing Internal Storage Tutorial

There are several ways to store and access data in Android. They are:

In this tutorial, I will demonstrate how to create and access a file from internal storage. Internal storage is the storage of the private data on the device memory.

Writing to a file

In order to open a file for writing, we call the method openFileOutput (String fileName, int mode). This method creates the file if the specified file does not exist. Below is the code snippet used to write to a file:

In the above code snippet, we use file open mode as Context.MODE_PRIVATE, so that the file can only be accessed by the calling application. message contains the text to be written to the file.

Reading a file

To read the file first we open the file using openFileInput(String fileName). Then we iterate over the fileInput, till the End of File (EOF) is reached. When the EOF is reached, read() method returns -1.

The User Interface

Let’s create a text box: to input and retrieve file data and 2 buttons: 1 to read from the file and other to write to file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_internal_storage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androiddeft.accessinternalstorage.InternalStorageActivity">

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="@string/app_title"
        android:textSize="28sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editTextContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textViewTitle"
        android:layout_margin="5dp"
        android:layout_marginTop="22dp"
        android:minLines="5">
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/readButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/editTextContent"
        android:layout_alignRight="@+id/editTextContent"
        android:layout_alignTop="@+id/writeButton"
        android:text="@string/read_button" />

    <Button
        android:id="@+id/writeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextContent"
        android:layout_alignStart="@+id/editTextContent"
        android:layout_centerVertical="true"
        android:text="@string/write_button" />
    />
</RelativeLayout>

The Complete Class

We will store the data in storagefile.txt. When the user clicks on write button, the contents of the EditText will be written into the internal storage. On the successful write to the internal storage, we clear the EditText so that, when the user clicks on the read button, the contents can be populated in the EditText.

If you have any queries you can write them down in the comments section.

Download Source CodeDownload APK

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Latest posts by Abhishek (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *