how to access getFilesDir() as an environment variable?

Elad Benda2 picture Elad Benda2 · Jan 19, 2014 · Viewed 9.1k times · Source

I want to access getFilesDir() which is a context method.

Is there any way to access it similarly to the way I access external-storage?

Environment.getExternalStorageDirectory();

meaning as an environment variable?

Maybe application static context? as I want to call this from a non-context class (same app service, but not an activity).

Answer

laalto picture laalto · Jan 19, 2014

It's not possible.

Context.getFilesDir() returns a path that is bound to your package and the Context is required to access the package name.

Environment is different as there's only constants that are common to all apps running on the same runtime.

However, a Context is available practically everywhere in an Android application so this shouldn't really be a problem.


Follow up:

how about Environment.getDataDirectory()? how can I get data\data as from data\data\com.Myapp using EnvironmentVar?

Environment.getDataDirectory() just returns the part of the data directory common to all apps. For example, the /data/data.

To get your own files dir (getFilesDir()), your package name and "/files" need to be appended to it. Context implementation does this for you.