java android pass resource file path to a method parameter
I have an xml file under res folder, the structure is res/raw/myfile.xml开发者_如何学编程. Now i am using JDOM for parsing the file using SAXBuilder.build("raw/myfile.xml"); And in Logcat it is showing that it cannot open the file. How can I achieve this? any suggestions?
This is how you parse a raw resource called myfile.xml
:
InputStream input = context.getResources().openRawResource(com.YourApp.R.raw.myfile);
Document doc = DocumentBuilder.parse(input);
Note1 Notice that there is no file extension at the end of com.YourApp.R.raw.myfile
! (so you cannot have two different files with same name)
Note2 You cannot make sub folders under raw folder!
Note3 If I remeber correctly, your file name should have all lower case and no spaces!
Maybe this'll help:
Referring to Android Resources Using URIs
As soon as you've built your URI you can easily get the path as a string and pass it to the parser. See URI documentation (method getPath)
精彩评论