Android External Storage – Reading, Writing, Saving Files
External storage in Android can be used to write and store data, read configuration files, etc. This article continues the tutorial on internal storage in Android.
Android External Storage
External storage like SD cards can also store application data. Generally, there are two types of external storage:
- Primary External Storage: Built-in, shared storage that is accessible via a USB cable and mounted as a drive on a host computer. Example: Nexus 5 with 32 GB.
- Secondary External Storage: Removable storage. Example: SD card.
All applications can read and write files on the external storage, and the user can remove them. We need to check if the SD card is available and if we can write to it. Only when the external storage is available can the application write to it; otherwise, the save button will be disabled.
Sample Project Structure for Android External Storage
First, we need to ensure that the application has permission to read and write data to the user’s SD card. To do this, open the AndroidManifest.xml and add the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Additionally, the external storage may be mounted by the user as a USB storage device. We need to check if the external storage is available and not read-only.
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false);
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState);
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(extStorageState);
}
Sample Code for Android External Storage
The layout of activity_main.xml is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Reading and Writing to External Storage"
android:textSize="24sp"/>
<EditText android:id="@+id/myInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" android:lines="5"
android:minLines="3" android:gravity="top|left"
android:inputType="textMultiLine">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:layout_marginTop="20dp">
<Button android:id="@+id/saveExternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE"
android:layout_weight="0.5"/>
<Button android:id="@+id/getExternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="READ" />
</LinearLayout>
<TextView android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Besides the buttons for saving and reading from external storage, we show the response of saving/reading in a TextView. The MainActivity.java class is given below:
package com.journaldev.externalstorage;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText inputText;
TextView response;
Button saveButton, readButton;
private String filename = "SampleFile.txt";
private String filepath = "MyFileStorage";
File myExternalFile;
String myData = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (EditText) findViewById(R.id.myInputText);
response = (TextView) findViewById(R.id.response);
saveButton = (Button) findViewById(R.id.saveExternalStorage);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(inputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
inputText.setText("");
response.setText("SampleFile.txt saved to External Storage...");
}
});
readButton = (Button) findViewById(R.id.getExternalStorage);
readButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData += strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
inputText.setText(myData);
response.setText("SampleFile.txt data retrieved from External Storage...");
}
});
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false);
} else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState);
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(extStorageState);
}
}
Environment.getExternalStorageState()
: Returns the path to the internal SD mount point, e.g., “/mnt/sdcard”.getExternalFilesDir()
: Returns the path to the file folder within Android/data/data/application_package/ on the SD card. This is used to store files that your app needs. Once the app is uninstalled, the data stored in this folder is also gone.
Make sure your Android emulator is configured to have an SD card. Go to Tools->Android->Android Virtual Device, edit configurations->Show Advanced Settings.