Difference between getExternalFilesDir and getExternalStorageDirectory() Difference between getExternalFilesDir and getExternalStorageDirectory() android android

Difference between getExternalFilesDir and getExternalStorageDirectory()


getExternalFilesDir()

It returns the path to files folder inside Android/data/data/your_package/ on your SD card. It is used to store any required files for your app (e.g. images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.

getExternalStorageDirectory()

It returns the root path to your SD card (e.g mnt/sdcard/). If you save data on this path and uninstall the app, that data won't be lost.


First of all, we need to understand what is difference between Internal Storage, External Storage (aka primary external storage), and Secondary External Storage?

Internal Storage: is storage that is not accessible by the user, except via installed apps (or by rooting their device). Example: data/data/app_packageName

Primary External Storage: In built shared storage which is "accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer". Example: When we say Nexus 5 32 GB.

Secondary External Storage: Removable storage. Example: SD Card.

getExternalFilesDir (String type)

It returns the path to files folder inside Android/data/data/your_package/ on primary external storage. Which is inbuilt storage.

Environment.getExternalStorageDirectory()

It will return the path of the secondary external storage directory


! IMPORTANT UPDATE ! for whoever comes across this question.

As this is a somewhat old question just wanted to provide some additional information. Since KitKat even apps that have WRITE_EXTERNAL_STORAGE permission are only allowed to write to Android/data/data/your_package/ on external storage, a.k.a getExternalFilesDir()

If you will try to write to getExternalStorageDirectory() + "/somefolder/anotherfolder/" you will get a SecurityException on most devices