Introduction
This article explains internal storage in Android.
Android provides many options to store application data. The choice depends on your needs such as whether the data should be accessible from other applications and how much space your data needs.
Internal storage
Store private data on the device memory. You can directly save your application data to the internal memory. By default, data is saved in internal memory and other applications cannot access it. When you uninstall your application all files will be removed from the device.
The FileInoutStream and FileOutPutStream classes are used to read and write the data of the files.
FileInputStream
FileInputStream gets input bytes from a file. The FileInputStream class provides the facility to read streams of raw bytes such as image data.
It extends the InputStream class. The following are methods provided by the fileInputStream class:
-
public int read(): this method reads a single byte from this Stream and returns this as an integer in the range from 0 to 225
-
public void close(): this method closes the stream.
FileOutPutStream
FileOutputStraem is an output stream for writing data to a file.
It extends the OutputStream class. Methods provided by the outputStream class are:
-
public void close(): This method is used to close the stream
- public void write(): This method is used to write a single byte to the stream
Step 1
Create a project like this:
Step 2
Create an XML file and write the following:
-
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
-
-
-
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_marginRight="20dp"
- android:layout_marginTop="24dp"
- android:ems="10" >
-
- <requestFocus />
- </EditText>
-
-
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/editText1"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="24dp"
- android:ems="10" />
-
-
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/editText1"
- android:layout_alignBottom="@+id/editText1"
- android:layout_alignParentLeft="true"
- android:text="File Name:" />
-
-
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/editText2"
- android:layout_alignBottom="@+id/editText2"
- android:layout_alignParentLeft="true"
- android:text="Data:" />
-
-
-
- <Button
- android:id="@+id/buttonSave"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="70dp"
- android:layout_marginTop="150dp"
- android:text="save" />
-
-
-
- <Button
- android:id="@+id/buttonRead"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="140dp"
- android:layout_marginTop="150dp"
- android:text="read" />
-
- </RelativeLayout>
Step 3
Create a Java class file and write the following:
A package is a group of classes, subpackages, and interfaces. I used various packages in this application.
I imported java.io.BuffeReader that provides the BufferReader class. This class provides the facility to read text from a character input stream.
java.io.FileInputStream provides the FileInputStream class that gets input bytes from a file. The FileInputStream class provides the facility to read streams of raw bytes such as image data.
It extends the InputStream class.
java.io.FileOutputStream provides the FileOutputStream class is an output stream for writing data to a file.
It extends the OutputStream class.
-
android.app.Activity provides methods such as onCreate() to create the activity and to display the activity on the screen.
-
android.widgets.buttons provide you the facility to use buttons in your Activity.
-
android.widgets.toast provides you the facility to use the Toast class in your application. Using the toast class you can use toast notification to display a message.
Step 4
Android Manifest.xml file
This is an Android manifest.xml file that works as a bridge between developers and the Android System. This file contains information about the package and the component.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.internalstorageapp"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.internalstorageapp.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Step 5
insert filename and date to save the data.