Storage in Android – How to save data and files in Android?

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this Android tutorial, we’ll learn about Storage in Android. We will see what are the different ways to save the application data in android, what is internal storage in android and what is external storage in android? Then we will learn how to save files in Android. So let’s begin our journey.

Storage in Android

Storage in Android

Android uses a file system that is like a disk-based file system of other platforms. The Android system provides different options to save application data:

  • Application-specific storage in Android
  • Shared storage in Android
  • Preferences in Android
  • Databases in Android

Let us go through these all one by one.

1. Android Application-specific storage

In this, the stored data is meant only for a specific application’s use. It is stored either in a dedicated directory inside the internal storage or in external storage. The sensitive data, that is specific to the app and shall not be accessible to other apps is generally stored in Internal Storage. There are certain things about it that are:

a. To access it, we have two ways:

  • Internal Storage
    getFilesDir() or getCacheDir()
  • External Storage
    getExternalFilesDir() or getExternalCacherDir()

b. Permission is not required for Internal Storage as well as for External Storage. When considering external storage, we do not need permission for Android version 4.4(API level 19) or higher.

c. If the app data is in internal storage, other apps cannot access it. If the data is in external storage, files can be accessed by other apps.

d. If the application is uninstalled by any means, the data files also get deleted.

2. Shared Storage in Android

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

In this, the stored data is meant to be shared among other apps as well. It includes data such as multimedia, documents, etc. There are certain things about it that are:

a. Access the data with MediaStore API and Storage Access Framework.

b. To read the media file use these permissions: READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE for Android 10 or higher.

c. You can access the media files by other files through READ_EXTERNAL_STORAGE and the document files, through system file picker.

d. If you uninstall the app, the data would not be deleted from the External Storage.

3. Android Preferences

They store the data in a private database. It stores the data in the form of a key-value pair. There are certain things about it that are:

a. Data is stored in a key-value pair.
b. It can be accessed through the Jetpack preference library.
c. Data from this cannot be accessed through other applications.
d. When you uninstall the app, the data gets deleted.

4. Android Databases

In this, the data is stored as structured data in a private database. For that, it uses the Room persistence library. There are certain things about it that are:

a. It has structured data
b. To access the data use Room persistence library
c. No other application can access the data
d. On the uninstallation of the application, the data gets deleted too.

Internal Storage in Android

Files can be saved directly in the “internal” storage of the device. When the files are saved in the internal storage, they are automatically set to private. These files are set to private so they cannot be used by other applications.

We can create and write a private file in the internal storage as follows:

a. Invoke the method, callFileOutput() with the name of the file and the correct mode. It’ll return the FileOutputStream.
b. Write in the file, using the write() method.
c. After writing in the file, close it using the close() method.

We can read from a file from internal storage as follows:

a. Invoke the method, callFileOutput() with the name of the file and the correct mode. It’ll return the FileInputStream.
b. Read the file using the read() method.
c. After reading, close the stream using the close() method.

For example:

To read –

FileOutputStream fOut = openFileOutput(“ file_name ”,MODE_INTERNAL_READ);
String name= “John”;

To write –

FileInputStream fIn = openFileInput(file_name);
Int x;
String data= “”; // it will hold the data
if( ( ! = -1){
data = data + Character.toString((char)ch);

There are certainly some other important methods, which are as mentioned below:

  • getFileDir()- It helps to get the absolute path to the directory where internal files are there.
  • getDir()- It opens an existing directory in the internal storage space. It also creates one, if it doesn’t exist.
  • deleteFile()- It deletes an existing file that was there on the internal storage.
  • fileList()- This method returns an array of the files currently saved by the application
  • write (byte[] buffer, int byteOffset, int byteCount)- It writes count bytes from the byte array buffer that starts from the position offset to the stream. It is for FileInputStream.
  • read (byte[] buffer, int byteOffset, int byteCount)- It reads the length bytes and stores them in the byte array that starts at the offset. It is for FileOutputStream.

External Storage in Android

You can store Files in the “external storage” of the device as well. External storage of a device can be a removable storage media(memory cards), or internal non-removable storage (such as RAM). When we store a file in the external file, it becomes accessible through other applications as well. These files do not get deleted if we uninstall the Application.

Accessing the files on the External Storage:

a. How to Open a file in Android?

To open a file that is in the external file, use the getExternalFilesDir() method. It’ll take a type parameter to specify the type of subdirectory that we need. The types can be such as DIRECTORY_ALARMS or DIRECTORY_MUSIC, etc. Pass null to receive the root of the application’s directory.

To open a file that represents the root of the external storage in the API level 7 or lower, you can use getExternalStorageDirectory(). If you want to write some data, then write the data in the following directory:
/Android/data/your package/files/

Here, your package need to be mentioned, like “com.dataflair.myandroidapp”.

b. How Save the files in Android?

To keep the files that are not specific to the application and should not be deleted with the application, you need to save them. You can store them in the directories of the external storage such as Music/, Movies/, Download/, and many more.

To save the file in API level 8 or greater you can use the getExternalStoragePublicDirectory() method. In this method, you can pass the type of public directory that you want to. You can use public directories such as DIRECTORY_RINGTONES, DIRECTORY_MUSIC, or some other. This can also create a directory if required.

For the API level lower than or equal to 7, use the getExternalStorageDirectory() method to open a File that represents the root of external storage, and later save the files in any one of the directories that are as below:

Pictures/Movies/ Download/Podcasts/


So, in this article, we have discussed the Storage method for Android. Here we saw what are the different options to store the data and the files for an Android application. We then discussed how we can store the files and data in the Internal and External storage of the device.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. Paul says:

    Helps me a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *