Android: Storing and Accessing File from External 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 external storage in Android.

Android External Storage

There are 2 types of external storages:

  • Primary External Storage: The inbuilt storage that can be accessed by connecting the phone to a PC via the USB cable. eg: Redmi Note 3 32GB
  • Secondary External Storage: Removable external SD card.

There is no security imposed in the file stored in external storage. It can be accessed by any application.

Access Permissions

To access external storage, Android requires you to specify android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE in AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.androiddeft.accessexternalstorage">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
		<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

		<activity android:name=".ExternalStorageActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />

				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>

</manifest>  

Checking Accessibility and Readability of External Storage

Before accessing external storage, we need to confirm that external storage is available and accessible. The 2 methods below accomplish this:

Writing data to External Storage

To create a file in external storage, we call the file constructor File(File parent, String child). Here, parent is the directory path and child is the file name. The returned file object is then passed to FileOutputStream(File file).

Reading data from External Storage

For reading the file from external storage, we pass File object to FileInputStream(File file). We iterate over FileInputStream object to read the contents of the file. When the End of File 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_external_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.accessexternalstorage.ExternalStorageActivity">

	<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

When the user clicks on Write button, the content typed in the EditText will be written into the exernal sirage directory: /storage/emulated/0/Android/data/com.androiddeft.accessexternalstorage/externaldir. Once the contents are written to the external storage, the EditText will be cleared so that when the user clicks on the Read button,
contents of the file can be read and shown in the EditText.

If you have any queries you can write them down in the comments section. You can download the source code and APK file from the below links.

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 *