Elad Benda2 Elad Benda2 - 11 days ago 6
Java Question

how to access getFilesDir() as an environment variable?

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

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.