Android fileinputstream引发filenotfoundexception?

Android fileinputstream引发filenotfoundexception?,android,filenotfoundexception,fileinputstream,Android,Filenotfoundexception,Fileinputstream,我正试图打开一个xml文件来解析它,但是我尝试打开该文件时被阻止了。我的代码是: try { return new FileInputStream(location); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return nu

我正试图打开一个xml文件来解析它,但是我尝试打开该文件时被阻止了。我的代码是:

        try {
            return new FileInputStream(location);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }   
其中location是传递到函数中的字符串。运行我的代码后,我得到:

java.io.FileNotFoundException:/C:/Users/Seth/AndroidWorkspace/ucoon/res/xml/information.xml(没有这样的文件或目录)


我给出了完整的文件路径,我没有错放任何东西。我做错了什么?

您无法从主机系统(在您的情况下,它似乎是Windows)访问文件。将文件复制到您的设备/仿真器SD卡上,然后从那里读取-不要忘记使用“写入外部存储”权限。

您无法从设备或仿真器读取pc上的文件


有关数据存储和访问资源的介绍(这可能是您感兴趣的,因为您正在尝试访问
res/xml/

您正在尝试使用PC上的真实路径加载文件。Emulator对您的文件系统一无所知

将xml放入项目文件夹
res/xml/
时,可以使用标准资源查找方式对其进行解析:

XmlResourceParser xpp=res.getXml(R.xml.myxml)

其中,myxml是不带扩展名的xml文件名

,我对此进行了研究,但无法理解它如何与我的代码相匹配。我实现了一个定制的xml解析器,与这里的第8个示例非常相似:我试图编写方法getInputStream(),这样我就可以使用“xml.parse(this.getInputStream(),xml.Encoding.UTF_8,root.getContentHandler());”这一行了。在本文中,xml文件是从网上下载的。所以这不是你的情况。方法
Resource.getXml()
返回对
XmlResourceParser
对象的引用。本文在“XML拉式解析器”一节中介绍了这种情况
XmlResourceParser
XmlPullParser
使用相同的接口