views:

468

answers:

2

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 Java I know I can use

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

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

+1  A: 

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.

Johann
The poster indicated that the problem is not having R.raw.myDataFile available.
CommonsWare
+1  A: 

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.

CommonsWare