如何从Android的资产文件夹的名字加载XML文件 [英] How to load an xml file from android's assets folder by name

查看:176
本文介绍了如何从Android的资产文件夹的名字加载XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这个方法通过名称来加载位于一个Android项目的/ assets文件夹中的XML文件:

I am trying to load an xml file located in the /assets folder of an android project by name using this method:

getAssets().openXmlResourceParser("thefilename.xml");

不过,始终执行此code抛出FileNotFound异常,即使该文件位于/资产的文件夹,并用正确的文件名。
现在,我还没有把文件中的/ RES / XML的文件夹,因为我真的需要能够1.编辑文件右侧的设备本身,最重要的2新的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.

推荐答案

我觉得你在找什么或者是 getAssets()。打开(thefilename.xml) getAssets()。openFd(thefilename.xml)取决于最终用途的文件是什么。您可以从戴安娜的在这个职位响应一段时间回来了 openXmlResourceParser()是不是真的有用刚刚获得的文件中的资产/目录中看到:的 http://goo.gl/2KfgT

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

从那里,你将有一个流,你可以喂成的SAXParser或者你选择的任何其他人。

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.

希望帮助!

这篇关于如何从Android的资产文件夹的名字加载XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆