43

I want to replace one context method with Environment variable.
But not sure if they mean the same.

What is the difference between

getFilesDir() and Environment.getDataDirectory() ?

How can I get data\data as a from of data\data\com.Myapp using Environment Variable?

Pradip
  • 3,189
  • 3
  • 22
  • 27
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • What did you mean by ~"**one context method**"? – IgorGanapolsky Jul 07 '16 at 14:08
  • I recommend you read this excellently written article explaining the difference: https://www.grokkingandroid.com/how-to-correctly-store-app-specific-files-in-android/ It's actually much easier than these answers and the docs would have you believe. – Joshua Pinter Jan 22 '18 at 06:20

4 Answers4

49

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

Community
  • 1
  • 1
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • how can i get `data\data` as from `data\data\com.Myapp` using EnvironmentVar? – Elad Benda2 Jan 20 '14 at 10:02
  • You can get path using EnvironmentVar as `Environment.getDataDirectory()`. Check out my answer i have mentioned a link at the end which will guide you. – GrIsHu Jan 20 '14 at 10:05
  • 1
    but it returns only `data` from `data\data\com.Myapp` – Elad Benda2 Jan 20 '14 at 10:12
  • As i stated in answer `getFilesDir ()` returns the application's directory path whereas `getExternalStorage` which will used in context of `Environment` will only return the user's directory path. The thing which you want to achieve is not possible using `Enviroment.getFilesDir()` as the method `getFilesDir()` does not resides inside Enviroment namespace. – GrIsHu Jan 20 '14 at 10:20
  • 2
    Try out this way `Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath()` – GrIsHu Jan 20 '14 at 10:22
  • @EladBenda2: Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/"; – ZP007 Mar 11 '22 at 22:58
18

Environment returns user data directory. And getFilesDir returns application data directory.

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Adam Radomski
  • 2,515
  • 2
  • 17
  • 28
  • how can i get `data\data` as from `data\data\com.Myapp` using EnvironmentVar? – Elad Benda2 Jan 20 '14 at 09:53
  • 1
    I don't understand what you mean? You want to cut `data\data` part from `data\data\com.Myapp` path? Use `split("\")` method on this path(as String) and then join particular parts. – Adam Radomski Jan 20 '14 at 09:58
  • i want `{this path}` from EnvironmebtVars object to fill `{this path}\com.Myapp\`in my example `data\data` – Elad Benda2 Jan 20 '14 at 10:31
2

getFilesDir()

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

Environment.getDataDirectory()

Return the user data directory.

Nambi
  • 11,944
  • 3
  • 37
  • 49
  • 4
    What may not be obvious is that the "user data directory" returned by getDataDirectory is the **system-wide** data directory (ie, typically so far /data) and *not* an application specific directory. Applications of course are not allowed to write to the overall data directory, but only to their particular folder inside it or other select locations whose owner has granted access. – Chris Stratton May 01 '15 at 18:13
-2

Try this

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath()
Bibin
  • 100
  • 4
  • No - this is meaningless. Environment.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 http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) – Chris Stratton May 01 '15 at 18:22