108

I understand that ExternalFiles is to be used on API 8 and up and getExternalStorageDirectory is for 7 and down. However I am a little confused between the use. For example I wanted to check that a folder that exists and previously you would use something like:

File ChildFolder = new File(Environment.getExternalStorageDirectory() + "/ParentFolder/Child");

However every example I see says to use getExternalFilesDir (null), File.ext. Since I am above API 8 I want to use this method but how do I just check for a folder? I will check for a files existence at another point but for now just want to see if the folders exist??

4 Answers 4

153
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.

11
  • 7
    Google Android wants you to use whatever they provide within the framework. So its up to your choice and design requirements. If you don't want the files to stay left when the app is uninstalled then your are encouraged to use getExternalFilesDir() or getExternalCacheDir()
    – waqaslam
    Apr 12, 2012 at 14:50
  • 5
    Its because in API 8, they introduced advanced media scanner features which allows to scan data from folders like music, pictures, ringtones, etc. Those directories are automatically created only if you use getExternalFilesDir(). Now since this feature wasn't available in API 7 that's why they recommended to use getExternalStorageDirectory(). Simple...
    – waqaslam
    Apr 12, 2012 at 19:31
  • 2
    its ok, give it some time, soon there will be an eureka moment :)
    – waqaslam
    Apr 13, 2012 at 5:56
  • 8
    It's important to know that getExternalStorageDirectory() does not necessarily -- maybe even not usually -- return the "path to your SD card." Instead it returns the "primary external storage directory". The docs (developer.android.com/reference/android/os/…) say:
    – LarsH
    Jun 22, 2017 at 14:11
  • 2
    'Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.'
    – LarsH
    Jun 22, 2017 at 14:11
100

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

10
  • Thanks for explaining the different types of storage. However, maybe edit the internal storage item to mention that the method call to get that value is getFilesDir(). Well, I guess that method actually returns data/data/app_packageName/files but it is still internal storage.
    – raddevus
    Nov 10, 2015 at 13:30
  • 3
    @VicJordan Please write about File getExternalStoragePublicDirectory (String type) and File getFilesDir () as well
    – AnV
    Jul 29, 2016 at 8:55
  • 2
    Thanks Vic. Environment.getExternalStorageDirectory() outputs /storage/emulated/0
    – gimmegimme
    Feb 10, 2017 at 4:05
  • Do you have any documentation for the statement that primary external storage is alway built-in, whereas secondary is always removable? I thought those were independent issues... some phones don't have built-in "external" storage, in which case the primary external storage might be the removable SD card.
    – LarsH
    Jun 5, 2017 at 16:52
  • 1
    This answer is wrong. getExternalStorageDirectory returns /storage/emulated/0 which is the Primary internal storage
    – MSeiz5
    Jul 19, 2019 at 11:41
23

! 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

7
  • 13
    According to the docs, apps that have the WRITE_EXTERNAL_STORAGE permission CAN write to other packages' external storage, not just their own. It's apps that do NOT have the permission that can only write to their own directory: developer.android.com/reference/android/content/… "Starting in KITKAT, no permissions are required to read or write to the returned path; it's always accessible to the calling app... To access paths belonging to other packages, WRITE_EXTERNAL_STORAGE and/or READ_EXTERNAL_STORAGE are required."
    – Oded
    Jun 12, 2016 at 22:30
  • This is where "on most devices" begins to matter as android docs generalizes the idea, but manufacturers sometimes have things like InternalStorage (built-in storage for app data), ExternalStorage-Primary (also built-in storage with a bigger access scope) and ExternalStorage-Secondary (this would be mounted stuff like SD cards). Now you can take a guess which path getExternalStorageDirectory returns
    – SGal
    Jun 14, 2016 at 9:05
  • 8
    That's doesn't address the incorrect information in your answer. "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()". Per the docs, this is wrong. With WRITE_EXTERNAL_STORAGE, apps can write to all external storage.
    – Oded
    Jun 19, 2016 at 6:31
  • 1
    @Oded, have you tested this? My understanding of the docs, and my experience with KitKat and later devices, is different from yours. (Wish I had time to hash out all the details now. Maybe later.) My experience is that SGal is right. Then there's also Lollipop which is a little different.
    – LarsH
    Jun 5, 2017 at 16:55
  • 3
    This answer is blatantly false, according to Android's documentation. See developer.android.com/reference/android/… "Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir()." (Note that API level 19 = KitKat.) No idea how it got so many upvotes.
    – Ajedi32
    Oct 5, 2017 at 16:39
16

!! IMPORTANT !!

Environment.getExternalStorageDirectory() is deprecated and Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT, should be used instead.

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

Also beginning from Android.M developers need to ask for permissions at run time.

See more details in documentation here and this question

Environment.getExternalStorageDirectory() deprecated in API level 29 java

3

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.