Introduction
This article explains External 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.
We can also store data to and read data from the external storage just like internal storage. The "FileInputStream" and "FileOutPutStream" classes are used to save and read data into the files
.
External Storage
Every Android device provides storage space to save files known as external storage. Data from the external storage can be removed and can be modifiable by the user
.
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 data into the files.
FileInputStream
The 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()
reads a single byte from this Stream and returns it as an integer in the range from 0 to 225.
-
public void close()
closes the stream.
FileOutPutStream
FileOutputStraem is an output stream for writing data to a file.
It extends the OutputStream class. The following are methods provided by the outputStream class:
Step 1
Step 2
Create an XML file with this:
- <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" >
-
- <LinearLayout
- android:id="@+id/linearlayout1"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_width="fill_parent">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="File Name:" />
-
-
- <EditText
- android:id="@+id/edtText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:ems="10" >
- </EditText>
-
- </LinearLayout>
- <LinearLayout
- android:layout_below="@+id/linearlayout1"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_width="fill_parent">
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Data:" />
-
- <EditText
- android:id="@+id/edtText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:ems="10" />
-
- </LinearLayout>
-
-
- <Button
- android:id="@+id/save_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="170dp"
- android:layout_marginTop="150dp"
- android:text="save" />
-
- <Button
- android:id="@+id/read_btn"
- android:layout_marginLeft="50dp"
- android:layout_marginTop="150dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="read" />
-
- </RelativeLayout>