Last Updated:

Android External Storage: An example of working with external memory

In the last article, we talked about the internal memory of an Android device and looked at an example of working with it. Today we will continue to get acquainted with data storage in Android on the example of External Storage (external memory of Android device).

Short review Android External Storage

An external storage device, such as an SD card, can store application data.
There are two types of external drives:

  1. Primary External Storage. Built-in shared storage will be available to the user if he connects a USB cable and mounts it as a disk on the computer.
  2. Secondary External Storage. Removable storage, such as an SD card

All applications can read and write data placed on an external storage device and the user can delete this data. To work with an external storage device, all you need to do is check the availability of the SD card.

Example of working with Android External Storage

Today we will write a simple application that will work with the external memory of the Android device. The structure of the project is no different from the simple Hello World application, which we wrote in the first lessons on Android. Create a simple project in Android Studio and start programming.

First of all, we need to get permission to read and write data located on external memory. To do this, add the following permissions:AndroidManifest.xml

Below is a layout of the file:activity_main.xml

As you can see from the layout, we put the text on the buttons and in the widget in the file (always do this):EditTextstrings.xml

The listing of the class is as follows:MainActivity

Note:

The method returns the path to the internal store at . Environment.getExternalStorageState()/mnt/sdcard

The method returns the path to the folder files on the SD card. It is used to store the files your application needs, such as images downloaded from the Internet or the application cache). It should be noted that with the removal of the application, all files stored in this folder will also be deleted.getExternalFilesDir()android/data/data/application_package/

 

Below is the result of working with External Storage on a real Android device.

 

First, enter some data into the field, then click "Save data", then clear the field and click "Read data":