External Storage in Android

Every android application needs a way to store data, Android offers a few ways to store data, which Includes:

  1. Internal Storage
  2. External Storage
  3. SharedPreferences
  4. SQLite Database
  5. Cloud Storage (via Network)

In this tutorial we will use External storage in android to store and read data into files.

External storage in Android (Such as SD Card) is Also used for storing data. We just need to check if the External storage is available or not, The method getExternalStorageState() is used to check if the External Storage is available or not. If it’s available then we can also read and write on External storage. To Read and Write on external Storage FileInputStream and FileOutputStream classes are used.

Initial Steps For Read and Write on External Storage

  1. Need a permission to read and write data on External Storage
  2. Also Need to check if the External Storage is Available or not
  • First We need a permission to read and write on external storage, for that we will declare permission in AndroidManifest.xml File
				
					<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
				
			
  • External storage in android may be available,  so we need to check if the external storage is available or not and is not read only
				
					  private static boolean isExternalStorageReadOnly() {  
  String extStorageState = Environment.getExternalStorageState();  
  if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {  
   return true;  
  }  
  return false;  
 }  
  private static boolean isExternalStorageAvailable() {  
  String extStorageState = Environment.getExternalStorageState();  
  if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {  
   return true;  
  }  
  return false;  
 }  
				
			

getExternalStorageState() is static method of Environment class which is used to check if the external storage is available or not and If it’s not read only

Write file on External Storage in Android

In Android, To write data on External Storage We will be using FileOutputStream. FileOutputStream is an outputstream for writing data streams of raw bytes to file and we will use the write method of FileOutputStream to write data of bytes on file.

Code:
				
					private void writeData(File file, String fileData) {
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(fileData.getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

				
			

Read file from External Storage in Android

In Android, To Read data from External Storage We will be using FileInputStream. FileInputStream class in Java is useful for reading data from a file in the form of a sequence of bytes

Code:
				
					private String readData(File file) {
       try {
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        StringBuilder stringBuilder = new StringBuilder();
        String text = null;

        while ((text = bufferedReader.readLine()) != null) {
            stringBuilder.append(text);
        }

        return stringBuilder.toString();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
				
			

Here, In readData() method InputStreamReader is used to convert the sequence of bytes into the character streams and BufferedReader class’s readLine() method is used to read data from file.        

Step by Step Code Implementation

Here, we have created a app which will write and read data from the file (External Storage in Android). 

File : activity_main.xml 

The activity_main.xml layout file contains two EditText, one for the filename and one to write data and two buttons to read data from the file and to write data on the file.  

The MainActivity contains the implementation of the reading and writing to files from the External – Storage.

MainActivity: Kotlin/Java File  

Output:

Download the final project for External storage in android example from below link : 

External Storage in Android Example Code

If you liked this article, then share it with your friends.

Related Tutorials

Anatomy of an android app
Anatomy of an Android App
  • July 18, 2025
  • Com 0

Anatomy of an Android App Anatomy of an Android app refers to its core structure and essential components that work…

Roomdatabase in android image
Mastering room database using kotlin: Complete Guide with CRUD Operations
  • April 4, 2025
  • Com 0

Local data storage is essential for building modern Android apps that work offline and store data persistently. Whether you’re creating…

Android SDK Image
Android SDK and It’s Components
  • March 27, 2025
  • Com 0

Android SDK stands for Android Software Development Kit which is developed by Google for Android Platform with the help of…

Android Architecture Image
Android Architecture
  • March 20, 2025
  • Com 0

Android architecture is the foundation and structure that makes an Android device work. Android architecture consists of layers that work together to run apps…

Content Provider in Android Feature Image
Content Provider in Android
  • February 7, 2025
  • Com 0

Content Provider in Android is a component of the Android application that is useful for sharing data with other apps.…

SQLite Database in Android
SQLite Database in Android
  • January 28, 2025
  • Com 0

Android app or any application is made of two things Data And Functionality. So In Any application we need a…

Get Your Free Java
Data Structures Cheat Sheet! 🎁

Download the ultimate cheat sheet covering the Top 10 Data Structures every Java developer must know — with real-life examples! Just enter your email and get the free pdf.

We respect your privacy. No spam. Read our privacy policy for more information.