开发者

How to get File within' package, in an Android application

开发者 https://www.devze.com 2022-12-07 23:52 出处:网络
I\'m am creating an Android application, but in order to have one of the functionalities working I need to read a predefined xml file whilst only knowing its name, not the R.id..

I'm am creating an Android application, but in order to have one of the functionalities working I need to read a predefined xml file whilst only knowing its name, not the R.id..

In normal Jav开发者_StackOverflow中文版a I know I can use

getClass().getClassLoader().getResource(xmlName)

but using the limited Android SDK thats not working, any knows how to solve this?


Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.


From the Data Storage Section in the android developer manual:

If you have a static file to package with your application at compile time, you can save the file in your project in res/raw/myDataFile, and then open it with Resources.openRawResource (R.raw.myDataFile). It returns an InputStream object that you can use to read from the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消