Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getExternalFilesDir and getExternalStorageDirectory()

Tags:

java

file

android

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

like image 383
GPGVM Avatar asked Apr 12 '12 12:04

GPGVM


People also ask

What is getExternalFilesDir?

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()

What does getExternalFilesDir return?

Returns the path of the directory holding application files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.


2 Answers

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.

like image 146
waqaslam Avatar answered Sep 22 '22 13:09

waqaslam


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

like image 43
Vikasdeep Singh Avatar answered Sep 20 '22 13:09

Vikasdeep Singh