Difference Between Getexternalfilesdir and Getexternalstoragedirectory()

Differences between different getdir() function in android

getExternalFilesDir(String type)

Returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns.

getExternalFilesDirs()

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns.

getExternalCacheDir()

Returns absolute path to application-specific directory on the primary shared/external storage device where the application can place cache files it owns.

getExternalCacheDirs()

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place cache files it owns.

getExternalStorageDirectory()

Return the primary shared/external storage directory.

getExternalStoragePublicDirectory(String type)

Get a top-level shared/external storage directory for placing files of a particular type.

getFilesDir()

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

these documentations will help you

getExternalStorageDirectory() to getExternalFilesDir()

It basically is possible, but the place of the external storage for your application is different on different devices (basically because some devices have the external as part of their integrated storage). I have taken the code below from somewhere on SO and it works for me:

private File getAbsoluteFile(String relativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), relativePath);
} else {
return new File(context.getFilesDir(), relativePath);
}
}

getFilesDir() vs Environment.getDataDirectory()

From the HERE and HERE

public File getFilesDir ()

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

public static File getExternalStorageDirectory ()

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

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.

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

If you want to get your application path use getFilesDir() which will give you path /data/data/your package/files

You can get the path using the Environment var of your data/package using the

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath(); which will return the path from the root directory of your external storage as
/storage/sdcard/Android/data/your pacakge/files/data

To access the external resources you have to provide the permission of WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE in your manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Check out the Best Documentation to get the paths of direcorty

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

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

Is getExternalFilesDir() private?

Is getExternalFilesDir() private?

No. Any user and any app (with READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE) can access external storage.

Is this 100% interchangeable if I just replace it with..

That won't compile due to imbalanced parentheses. Also, the values are different (PICTURES versus MUSIC). Ignoring that, it is "interchangeable" insofar as they will both work and will both be on external storage. However, they are not the same thing. The first one is the user's standard PICTURES directory; the second one is a directory dedicated for pictures that is unique to your app. Also, the former requires WRITE_EXTERNAL_STORAGE on all devices; the latter requires it only for Android 4.3 and below.

I don't want them to be publicly visible to 99% of users (I'm excluding rooted devices or special cases)

Use internal storage (e.g., getFilesDir()).

Environment.getExternalStorageDirectory() deprecated in API level 29 java

Use getExternalFilesDir(), getExternalCacheDir(), or getExternalMediaDirs() (methods on Context) instead of Environment.getExternalStorageDirectory().

Or, modify mPhotoEditor to be able to work with a Uri, then:

  • Use ACTION_CREATE_DOCUMENT to get a Uri to a location of the user's choosing, or

  • Use MediaStore, ContentResolver, and insert() to get a Uri for a particular type of media (e.g., an image) — see this sample app that demonstrates doing this for downloading MP4 videos from a Web site

Also, note that your Uri.fromFile with ACTION_MEDIA_SCANNER_SCAN_FILE should be crashing on Android 7.0+ with a FileUriExposedException. On Android Q, only the MediaStore/insert() option will get your content indexed by the MediaStore quickly.

Note that you can opt out of these "scoped storage" changes on Android 10 and 11, if your targetSdkVersion is below 30, using android:requestLegacyExternalStorage="true" in the <application> element of the manifest. This is not a long-term solution, as your targetSdkVersion will need to be 30 or higher sometime in 2021 if you are distributing your app through the Play Store (and perhaps elsewhere).



Related Topics



Leave a reply



Submit