how to access getFilesDir() as an environment variable?

吃可爱长大的小学妹 提交于 2019-11-30 00:46:23

问题


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


回答1:


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.




回答2:


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:



来源:https://stackoverflow.com/questions/21216943/how-to-access-getfilesdir-as-an-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!