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