109

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??

5 Answers 5

156
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
    Commented 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
    Commented Apr 12, 2012 at 19:31
  • 2
    its ok, give it some time, soon there will be an eureka moment :)
    – waqaslam
    Commented 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
    Commented 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
    Commented 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
    Commented Nov 10, 2015 at 13:30
  • 4
    @VicJordan Please write about File getExternalStoragePublicDirectory (String type) and File getFilesDir () as well
    – AnV
    Commented Jul 29, 2016 at 8:55
  • 2
    Thanks Vic. Environment.getExternalStorageDirectory() outputs /storage/emulated/0
    – gimmegimme
    Commented 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
    Commented Jun 5, 2017 at 16:52
  • 1
    This answer is wrong. getExternalStorageDirectory returns /storage/emulated/0 which is the Primary internal storage
    – MSeiz5
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented Oct 5, 2017 at 16:39
17

!! 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

4
  • if anyone is looking for a stack overflow link and not something copy pasted from google docs look here stackoverflow.com/questions/57116335/… Commented Mar 6, 2020 at 2:59
  • 6
    Starting in Android 11, apps cannot create their own app-specific directory on external storage. Commented Nov 12, 2020 at 21:16
  • Apps *technically* cannot, but system can do this for them: stackoverflow.com/questions/64044993/…
    – mirh
    Commented Nov 14, 2021 at 23:33
  • Is there a reason you don't mention getExternalStoragePublicDirectory as an alternative, for apps targeting API 33+? It's deprecated since v29 I guess, but still works, and is the best I've found for storing app logs in a place users can access with file managers. I do: val docsFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).absoluteFile; val myAppDir = File(docsFolder, "MyApp"); val myAppLogFile = File(directory, "app_log.txt");
    – Venryx
    Commented Jul 6 at 15:42
0

getExternalCacheDir()

Returns absolute path to application-specific directory on the primary shared/external storage device where the application can place cache files it owns. you can see it in androi document https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)

Your Answer

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

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