How to load an xml file from android's assets folder by name
I am trying to load an xml file located in the /assets folder of an android project by name using this method:
getAssets开发者_JS百科().openXmlResourceParser("thefilename.xml");
However, executing this code always throws a "FileNotFound" exception, even though the file is located in the /assets folder and is with the correct file name. Now, I have not put the file in the /res/xml folder because I really need to be able to 1. edit the file right on the device itself and most importantly 2. add new xml files to the application without issuing an update, to allow for easy user modifications. Thanks in advance.
I think what you are looking for is either getAssets().open("thefilename.xml")
or getAssets().openFd("thefilename.xml")
depending on what the end use of the file is. You can see from Dianne's response in this post awhile back that openXmlResourceParser()
is not really usable just to gain access to files in the assets/ directory: http://goo.gl/2KfgT
From there you will have a stream that you could feed into a SAXParser or do whatever else you choose.
Side Note: On the points you mentioned you really can't edit files directly in assets/ or add new files to assets/ at runtime. You will need to work with files on either internal or external storage to do those things. You probably already knew that, but I thought I'd mention it.
Hope that Helps!
精彩评论