Essential Android Storing Files in Internal and External Storage edit forum

Android Internal and External Storage - Terminology Clarification

Android developers(mainly beginners) have been confused regarding Internal & External storage terminology. There are lot of questions on Stackoverflow regarding the same. This is mainly because of the fact that terminology according to Google/official Android documentation is quite different to that of normal Android OS user. Hence I thought documenting this would help.

What we think - User’s Terminology (UT)

Internal storage(UT)| External storage(UT)| —— | —— | phone’s inbuilt internal memory| removable Secure Digital(SD) card or micro SD storage|Example: Nexus 6P’s 32 GB internal memory.|Example: storage space in removable SD cards provided by vendors like samsung, sandisk, strontium, transcend and others|

But, According to Android Documentation/Guide - Google’s Terminology (GT)

Internal storage(GT):

By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

External storage(GT):

This can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

External Storage(GT) can be categorized into two types:

| Primary External Storage | Secondary External Storage or Removable storage(GT)| | —— | —— | | This is same as phone’s inbuilt internal memory (or) Internal storage(UT)| This is same as removable micro SD card storage (or) External storage(UT)| Example: Nexus 6P’s 32 GB internal memory.|Example: storage space in removable SD cards provided by vendors like samsung, sandisk, strontium, transcend and others| This type of storage can be accessed on windows PC by connecting your phone to PC via USB cable and selecting Camera(PTP) in the USB options notification.|This type of storage can be accessed on windows PC by connecting your phone to PC via USB cable and selecting File transfer in the USB options notification.|

In a nutshell,

External Storage(GT) = Internal Storage(UT) and External Storage(UT)

Removable Storage(GT) = External Storage(UT)

Internal Storage(GT) doesn’t have a term in UT.

Let me explain clearly,

Internal Storage(GT): By default, files saved to the internal storage are private to your application and other applications cannot access them. Your app user also can’t access them using file manager; even after enabling “show hidden files” option in file manager. To access files in Internal Storage(GT), you have to root your Android phone. Moreover, when the user uninstalls your application, these files are removed/deleted.

So Internal Storage(GT) is NOT what we think as Nexus 6P’s 32/64 GB internal memory

Generally, Internal Storage(GT) location would be: /data/data/your.application.package.appname/someDirectory/

External Storage(GT):

Every Android-compatible device supports a shared “external storage” that you can use to save files. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

External Storage(GT) location: It could be anywhere in your internal storage(UT) or in your removable storage(GT) i.e. micro SD card. It depends on your phone’s OEM and also on Android OS version.

In order to read or write files on the External Storage(GT), your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions.

For example:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.

In External Storage(GT), you may also save files that are app-private

But,

When the user uninstalls your application, this directory and all its contents are deleted.

When do you need to save files that are app-private in External Storage(GT)?

If you are handling files that are not intended for other apps to use (such as graphic textures or sound effects used by only your app), you should use a private storage directory on the external storage
Beginning with Android 4.4, reading or writing files in your app’s private directories does not require the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions. So you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:
<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     android:maxSdkVersion="18" />
    ...
</manifest

Methods to store in Internal Storage(GT):

Both these methods are present in Context class

File getDir (String name, int mode)

File getFilesDir ()

Methods to store in Primary External Storage i.e. Internal Storage(UT):

File getExternalStorageDirectory ()

File getExternalFilesDir (String type)

File getExternalStoragePublicDirectory (String type)

In the beginning, everyone used Environment.getExternalStorageDirectory() , which pointed to the root of Primary External Storage. As a result, Primary External Storage was filled with random content.

Later, these two methods were added:

  1. In Context class, they added getExternalFilesDir(), pointing to an app-specific directory on Primary External Storage. This directory and its contents will be deleted when the app is uninstalled.
  2. Environment.getExternalStoragePublicDirectory() for centralized places to store well-known file types, like photos and movies. This directory and its contents will NOT be deleted when the app is uninstalled.

Methods to store in Removable Storage(GT) i.e. micro SD card

Before API level 19, there was no official way to store in SD card. But, many could do it using unofficial libraries or APIs.

Officially, one method was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).

File[] getExternalFilesDirs (String type)
It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

That means, it will return paths to both types of External Storage(GT) - Internal memory and Micro SD card. Generally second path would be storage path of micro SD card(but not always). So you need to check it out by executing the code with this method.

Example with code snippet:

I created a new android project with empty activity, wrote the following code inside

protected void onCreate(Bundle savedInstanceState) method of MainActivity.java

File internal_m1 = getDir("custom", 0);
File internal_m2 = getFilesDir();

File external_m1 =  Environment.getExternalStorageDirectory();

File external_m2 =  getExternalFilesDir(null);
File external_m2_Args = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

File external_m3 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

File[] external_AND_removable_storage_m1 = getExternalFilesDirs(null);
File[] external_AND_removable_storage_m1_Args = getExternalFilesDirs(Environment.DIRECTORY_PICTURES);

After executing above code,

Output:

internal_m1: /data/data/your.application.package.appname/app_custom

internal_m2: /data/data/your.application.package.appname/files

external_m1: /storage/emulated/0

external_m2: /storage/emulated/0/Android/data/your.application.package.appname/files

external_m2_Args: /storage/emulated/0/Android/data/your.application.package.appname/files/Pictures

external_m3: /storage/emulated/0/Pictures

external_AND_removable_storage_m1 (first path):
/storage/emulated/0/Android/data/your.application.package.appname/files

external_AND_removable_storage_m1 (second path):    
/storage/sdcard1/Android/data/your.application.package.appname/files

 external_AND_removable_storage_m1_Args (first path):
/storage/emulated/0/Android/data/your.application.package.appname/files/Pictures

external_AND_removable_storage_m1_Args (second path): /storage/sdcard1/Android/data/your.application.package.appname/files/Pictures

Note: I have connected my phone to Windows PC; enabled both developer options, USB debugging and then ran this code. If you do not connect your phone; but instead run this on Android emulator, your output may vary. My phone model is Coolpad Note 3 - running on Android 5.1

Storage locations on my phone:

Micro SD storage location: /storage/sdcard1

Internal Storage(UT) location: /storage/sdcard0.

Note that /sdcard & /storage/emulated/0 also point to Internal Storage(UT). But these are symlinks to /storage/sdcard0.

To clearly understand different storage paths in Android, Please go through this answer

Disclaimer: All the storage paths mentioned above are paths on my phone. Your files may not be stored on same storage paths. Because, the storage locations/paths may vary on other mobile phones depending on your vendor, manufacturer and different versions of Android OS.


Table Of Contents
19 Storing Files in Internal and External Storage
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close