External Storage in Android with Example
Android gives various options for storing apps data which uses a file system similar to the disk-based system on computer platforms
- App-Specific storage: Store data files within internal volume directories or external. These data files are meant only for the app’s use. It uses internal storage directories to save sensitive information such as a username and password that other app should not access.
- Shared Storage: Store data files such as images, audio, video, documents, etc. that the app may need to share with other apps.
- Shared Preferences: Store primitive data type such as integer, float, boolean, string, long in key-value pairs.
- Databases: Store structured data such as user-information(name, age, phone, email, address, etc.) into private databases.
Developers are advised to use the options available to store data depending upon the space required, reliable data access, and privacy of data. The data files saved over external storage devices are publicly accessible on shared external storage using USB mass storage transfer. Data files stored over external storage using a FileOutputStream object and can be read using a FileInputStream object.
External Storage Availability
In order to avoid crashing the app first, we need to check storage SD Card is available for reading and write operations. The method getExternalStorageState() is used to determine the state of mounted storage media such as SD Card is missing, read-only or readable, and writable. Below is the code snippet which we will use to check the availability of external storage.
Methods to Store data in External Storage
- getExternalStoragePublicDirectory(): This is the present recommended method to keep files public and these files are not deleted even when the app is uninstalled from the system. For eg: Images clicked by the camera are still available even after we uninstall the camera.
- getExternalFilesDir(String type): This method is used to store private data that are specific to the app only. And data are removed as we uninstall the app.
- getExternalStorageDirectory(): This method is not recommended. It is now absolute and it is used to access external storage in older versions, API Level less than 7.
In this example, we would store text data into the external storage and fetch to see that data. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Access Permission to External Storage
To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest.xml file. Add these permissions just after the package name.
Step 3: Before creating the layout and corresponding java files let’s add a few string attributes which we are using in our layout files
Go to app > res > values > string.xml and insert the following code snippet
Now go to app > res > values > colors.xml and change the color attributes as following in order to make App Bar more attractive.
Again go to app > res > drawable and create a new Drawable Resource File and name it as button_layout. In this, we are modifying our button style for a better UX/UI.
Step 4: Working with the activity_main.xml file
Go to res > layout > activity_main.xml and write down the following code. In this layout file, we are creating a multiline EditText View for getting the data from users and Buttons to save that data over internal and external storage media.
Step 5: Working with the MainActivity.java file
In MainActivity we define the functions that handled the onClick behavior of the buttons. And fetch the data from the EditText and save it in external storage publicly and privately. We also display a Toast message of the path where the data is stored.
Step 6: Create a new Empty Activity
We will create a new activity and name it ViewInformationActivity. We use this activity to display the saved data from the external storage. So, first, we create a layout for this activity similar to the MainActivity layout. activity_view_information.xml layout code snippet:
Now, we will create a corresponding java code for ViewInformationActivity. In this, we define methods showPublicData() and showPrivateData() over buttons that will fetch the data from the files saved to external storage and add the data to buffer, and then populate the data to TextView in order to show them. ViewInformationAcitity.java code snippet: