how to access getFilesDir() as an environment vari

2019-02-11 23:47发布

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

2条回答
Rolldiameter
2楼-- · 2019-02-11 23:56

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-12 00:03

There is an excellent documentation on this done by a Githib user, which is really valuable. Maybe it will help someone:

https://gist.github.com/granoeste/5574148

Here is the entire contents posted from the documentation:

enter image description here

enter image description here

查看更多
登录 后发表回答