Eftekhari
Eftekhari

Reputation: 1021

Difference between Environment.getExternalStorageDirectory() and Environment.getExternalStorageDirectory().getAbsolutePath()

What's the difference between

Environment.getExternalStorageDirectory() 

and

Environment.getExternalStorageDirectory().getAbsolutePath()

or

Environment.getExternalStorageDirectory().getCanonicalPath()

or

Environment.getExternalStorageDirectory().getPath()

I know what getAbsolutePath(), getCanonicalPath() and getPath() are.

I see in some examples Environment.getExternalStorageDirectory() is used like this:

getExternalStorageDirectory() + "/somefolder/anotherfolder/"

What confuses me is what getExternalStorageDirectory() itself as default refers to and why in some cases people use extra methods and when it is alright to use simply getExternalStorageDirectory() without anything extra to it?

Upvotes: 0

Views: 2298

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006624

I know what getAbsolutePath(), getCanonicalPath() and getPath() are.

Then you already know the answer to your original question ("What's the difference between..."), since the only thing that are different are those calls.

what getExternalStorageDirectory() itself as default refers to

It refers to the root of external storage. When the user mounts their Android device via a USB cable and gets a drive letter or volume or something, that drive points to the directory that getExternalStorageDirectory() points to.

Generally speaking, developers should not be using getExternalStorageDirectory(), but instead getExternalFilesDir() and kin on Context, or getExternalStoragePublicDirectory() on Environment.

why in some cases people use extra methods

Well, you have to create File objects pointing at some location on external storage somehow. While it is better to use the proper File constructor for that, some lazy programmers use string concatenation. And for that, you need a string.

when it is alright to use simply getExternalStorageDirectory() without anything extra to it?

Never, or close to it, simply because it is not a useful value on its own. That's akin to asking when you would use C: in Windows programming. That's a drive letter, and rarely is a simple drive letter on its own sufficient. We need a path to a directory or file, and for that, we need to add stuff to C: (at minimum, a backslash).

Upvotes: 2

Sebastian Pakieła
Sebastian Pakieła

Reputation: 3029

According to docs one returns File Object, and getAbsolutePath() is File class method.

Upvotes: 0

Related Questions