GetExternalFilesDir 和 getExternalStorageDirectory()之间的区别

我知道 ExternalFiles 将用于 API 8和更高版本,而 getExternalStorageDirectory 用于7和更低版本。不过我有点搞不清楚这两者之间的用法。例如,我想检查一个文件夹是否存在,以前您会使用如下内容:

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

但是我看到的每个例子都说要使用 getExternalFilesDir (null) ,File.ext。因为我的 API 8以上,我想使用这个方法,但我怎么才能检查一个文件夹?我将在另一个点检查文件是否存在,但现在只想看看文件夹是否存在? ?

117829 次浏览
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.

! 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

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

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