Android XML 解析错误 - “无法打开.../directory/container.xml”
我正在尝试解析 Android 设备上 sdCard 上的 XML 文件。
这段代码 -
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse("/mnt/sdcard/Speedr/pg55/META-INF/container.xml", xmlHandler);
输出这个错误 -
无法打开/mnt/sdcard/Speedr/pg55/META-INF/container.xml
原因:java.net.MalformedURLException:找不到协议:/mnt/sdcard/Speedr/pg55/META-INF/container.xml
对于为了简单起见,我更改了 sp.parse() 位置,但这正是实际代码中传递给该方法的位置。我还拿走了它周围的 try/catch 括号。
该文件肯定在那里,如果我在手机或计算机上浏览到它,我可以看到它,并且位置也是正确的,因为它没有给我 FileNotFound 错误。
我最初使用独立 Java 构建了此代码,并尝试将其实现到 Android 设备上,但遇到了此错误。错误日志确实没什么帮助。
我非常感谢这里任何人提供的任何帮助。
I am trying to parse an XML file that is sitting on the sdCard on my android device.
This piece of code -
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse("/mnt/sdcard/Speedr/pg55/META-INF/container.xml", xmlHandler);
Outputs this error -
Couldn't open /mnt/sdcard/Speedr/pg55/META-INF/container.xml
Caused by: java.net.MalformedURLException: Protocol not found: /mnt/sdcard/Speedr/pg55/META-INF/container.xml
For the sake of simplicity, I have changed the sp.parse() location, but this is exactly what is being handed to the method in the real code. I have also taken away the try/catch brackets that should surround it.
The file is most definitely there, I can see it if I browse to it on my Phone or Computer and the location is also correct since it is not giving me a FileNotFound error.
I originally built this code in standalone Java and have been trying to implement it onto an Android device and ran into this error. The error log really is less than helpful.
I would greatly appreciate any help I can get from anyone here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请确保您的
androidManifest.xml
中包含以下权限,以防您想要修改您的 xml,或保存其他:并将代码更改为:
这样您就可以将文件传递给解析器,因此您不需要指定
file://
协议。尝试不使用静态路径。外部存储路径根据设备而变化。
使用
Environment.getExternalStorageDirectory()
是很好的做法。您可能还想检查
xml
文件是否存在等,但基本上这就是它应该工作的方式。请参阅 Android 开发者这篇关于使用外部存储的文章 ,进一步检查存储状态等。
Please make sure, that you have the following permission included in your
androidManifest.xml
, in case that you'd like to modify your xml, or save an other:and change your code to:
This way you're passing a file to the parser, so you don't need to specify the
file://
protocol.Try not using static path. The external storage path changes based on devices.
Using
Environment.getExternalStorageDirectory()
is the good practice.You might also want to check whether the
xml
file exists, etc., but basically this is the way it should work.Please see this article about using external storage on Android Developers, for further checks about the storage state, etc.
您是否在清单中启用或添加了 SD 卡权限?
Did you enable or add SD Card permissions in the manifest?
您必须指定 http、ftp、文件等协议。在这里,您从 sdcard 访问它,因此整个路径之前将是
"file://"
。现在用这个修改你的代码片段,
You have to specify protocol like http, ftp, file etc. Here you are accessing it from sdcard so it would be
"file://"
before whole path.Now modify your code snippet with this,