Java Android库在读取openRawResource提供的流时遇到问题
我正在使用plist库加载plist文件: 我使用的代码如下:Java Android库在读取openRawResource提供的流时遇到问题,java,android,file,plist,Java,Android,File,Plist,我正在使用plist库加载plist文件: 我使用的代码如下: //InputStream ins = getResources().openRawResource(R.raw.skillsanddrills) InputStream ins = getResources().openRawResource(R.xml.skillsanddrills); //file name is skillsanddrills.plist NSDictionary rootDict;
//InputStream ins = getResources().openRawResource(R.raw.skillsanddrills)
InputStream ins = getResources().openRawResource(R.xml.skillsanddrills); //file name is skillsanddrills.plist
NSDictionary rootDict;
try {
rootDict = (NSDictionary)PropertyListParser.parse(ins);
...
然而,我得到:
java.lang.UnsupportedOperationException:给定的数据既不是
二进制或XML属性列表。ASCII属性列表不可用
支持
我不认为这是库的错误,因为我在使用另一个plist库时遇到了类似的错误,而文件本身只是一个plan XML结构。为什么Android会更改我的plist文件?
有没有办法解决这个问题
库还接受文件而不是流。但无法确定如何创建文件路径。这应该是它崩溃的源代码:
/**
* Parses a property list from a file. It can either be in XML or binary format.
* @param f The property list file
* @return The root object in the property list
* @throws Exception If an error occurred while parsing
*/
public static NSObject parse(File f) throws Exception {
FileInputStream fis = new FileInputStream(f);
String magicString = new String(readAll(fis, 8), 0, 8);
fis.close();
if (magicString.startsWith("bplist00")) {
return BinaryPropertyListParser.parse(f);
} else if (magicString.startsWith("<?xml")) {
return XMLPropertyListParser.parse(f);
} else {
throw new UnsupportedOperationException("The given data is neither a binary nor a XML property list. ASCII property lists are not supported.");
}
}
如果失败,请将其放入资产
,并按如下方式加载:
getResources().openRawResource(R.raw.skillsanddrills)
getAssets().open("filename");
如果失败,那么您的plist可能只是格式错误。这应该是它崩溃的源代码:
/**
* Parses a property list from a file. It can either be in XML or binary format.
* @param f The property list file
* @return The root object in the property list
* @throws Exception If an error occurred while parsing
*/
public static NSObject parse(File f) throws Exception {
FileInputStream fis = new FileInputStream(f);
String magicString = new String(readAll(fis, 8), 0, 8);
fis.close();
if (magicString.startsWith("bplist00")) {
return BinaryPropertyListParser.parse(f);
} else if (magicString.startsWith("<?xml")) {
return XMLPropertyListParser.parse(f);
} else {
throw new UnsupportedOperationException("The given data is neither a binary nor a XML property list. ASCII property lists are not supported.");
}
}
如果失败,请将其放入资产
,并按如下方式加载:
getResources().openRawResource(R.raw.skillsanddrills)
getAssets().open("filename");
如果这样做失败,那么您的plist可能只是格式错误。我以为我做了getResources()。openRawResource(R.raw.skillsanddrills)我想我没有。谢谢你的回答!好吧,这很奇怪,当我将更多plist文件添加到原始文件夹时,我会遇到相同的错误。。。现在完全糊涂了。我还修复了这个问题,我在原始文件夹和xml文件夹中都有另一个名为laws.plist的文件,当我删除xml文件夹版本(不再使用该文件)时,skillsandtrills.plist再次正确。很奇怪,但至少它又起作用了。。。hehI以为我做了getResources()。openRawResource(R.raw.skillsanddrills)我想我没有heh。谢谢你的回答!好吧,这很奇怪,当我将更多plist文件添加到原始文件夹时,我会遇到相同的错误。。。现在完全糊涂了。我还修复了这个问题,我在原始文件夹和xml文件夹中都有另一个名为laws.plist的文件,当我删除xml文件夹版本(不再使用该文件)时,skillsandtrills.plist再次正确。很奇怪,但至少它又起作用了。。。呵呵