Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getExternalStorageDirectory() and getDataDirectory()

Tags:

I am trying to get the Internal and External Storage path of an Android device.

This is how I am getting the Internal Storage path in most of the devices

Environment.getExternalStorageDirectory().getPath()

The above method is working in most of the device but some device this method is giving issue that is it is returning the SD Card path.

Someone who could tell me how to exactly get the Internal storage path in those device whose Environment.getExternalStorageDirectory().getPath() return External Storage path (SD Card path).

I am also aware of Environment.getDataDirectory().getPath() but this method return value like /data whereas the Environment.getExternalStorageDirectory().getPath() for the same device returns /storage/sdcard0 which points to Internal Storage when SD Card is not present but when an SD Card is present it gives the path which points to SD Card and not Internal Storage.

Can anyone help me understand the difference between the above two and also help me to get the Internal Storage path of an Android device? Also, help me with a solution which works sam across all the devices.

EDIT : Anyone who feels this post is inappropriate please comment and let me know the reason so that I could keep that in mind for future.

like image 704
Rahulrr2602 Avatar asked Jan 21 '18 16:01

Rahulrr2602


People also ask

What is getDataDirectory?

getDataDirectory() is an internal system-wide folder, not something you should be using with external storage. You are just abusing its name as a random custom directory type, but you should be using one of the type constants defined in Environment. See developer.android.com/reference/android/content/…

What is getExternalStorageDirectory?

getExternalStorageDirectory() Path to a directory where the application may access top level storage. The current operating system should be determined before issuing this function call, as this functionality is only available on Android.

What can I use instead of getExternalStoragePublicDirectory?

then you have to use getExternalStorageDirectory() instead of getExternalStoragePublicDirectory() . Example: If you want to create a directory in the internal storage if not exists.

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


1 Answers

I am referring to the Storage where generally files are saved such as WhatsApp Folder, DCIM, Download etc.

That is what the Android SDK refers to as external storage.

The above method is working in most of the device but some device this method is giving issue that is it is returning the SD Card path.

It should return the location of external storage on 100% of Android devices. Whether external storage is removable or not is up to the device manufacturer. The vast majority of Android devices have permanent non-removable external storage, typically on the same partition as internal storage in the on-board flash memory of the device.

Someone who could tell me how to exactly get the Internal storage path in those device whose Environment.getExternalStorageDirectory().getPath() return External Storage path (SD Card path).

Most likely, there is no such path.

the Environment.getExternalStorageDirectory().getPath() for the same device returns /storage/sdcard0 which points to Internal Storage when SD Card is not present but when an SD Card is present it gives the path which points to SD Card and not Internal Storage.

The value of getExternalStorageDirectory() should not vary based on the presence or absence of some piece of removable media. If your description is accurate, then that is a buggy device, and there is little that you can do about it. Certainly, there is no standard means of getting the "other" getExternalStorageDirectory() value, since that value should not be changing.

(BTW, what device do you have that behaves this way?)

Can anyone help me understand the difference between the above two

getDataDirectory() more or less returns the root of internal storage. I say "more or less" as apps never really work with this directory, but instead with app-specific subdirectories (e.g., getFilesDir() on Context).

like image 89
CommonsWare Avatar answered Sep 20 '22 12:09

CommonsWare