Android: Storing and Accessing File from External Storage Tutorial

There are several ways to store and access data on 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.

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.

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 loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

Leave a Reply

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