How to get File within' package, in an Android application
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.
精彩评论