找不到Android档案路径
2019年12月25日
作者:佚名
我试图做XML解析程序.
我也将FileInputStream用于我的XML文件.
我将XML文件放在android的资产文件夹META-INF文件夹下.
该文件名为“ container.XML”.
这是我的代码parseXML,
public void parseXMLinfoBook() throws FileNotFoundException,ParserConfigurationException,SAXException{ FileInputStream in = new FileInputStream("file:///android_asset/META-INF/container.xml"); StringBuffer inLine = new StringBuffer(); InputStreamReader isr = new InputStreamReader(in); BufferedReader inRd = new BufferedReader(isr); SAXParserFactory spf=SAXParserFactory.newInstance(); SAXParser spr=spf.newSAXParser(); XMLReader xmlreader = spr.getXMLReader(); XmlHandler xmlhe=new XmlHandler(); xmlreader.setContentHandler(xmlhe); }
这是Button.SetonClick代码,
public void onClick(View v) { // TODO Auto-generated method stub try { parseXMLinfoBook(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); tv.setText("ErrorPath "+e.getMessage()); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } } });
我只有错误消息.
希望对您有所帮助!
最佳答案
我假设您已将container.xml文件放置在apk包内应用程序的资产目录中.
要打开Android应用程序/ assets目录中的文件,您需要输入AssetManager.getAssets()在Context对象上可用,因此可用于您的Activity或Service.
AssetManager mgr = getContext().getAssets(); InputStream in = mgr.open("META-INF/container.xml"); InputStreamReader isr = new InputStreamReader(in); //... Rest of the code
以上是来客网为你收集整理的找不到Android档案路径 全部内容,希望文章能够帮你解决找不到Android档案路径 所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。