我有一个查看android资产的函数,如果资产存在则加载。 如果不然后查看文件系统。如果找到它,它会加载它 - 如果文件不是零字节。 如果在文件系统上找不到它,则会尝试从服务器获取它。 除非文件已经存在并且大小为0,否则所有这些都有效 - 并且它在某些“原始”java逻辑测试中失败。我不明白这里有什么问题,并探讨它是否是我自己代码之外的错误的可能性。
问题发生在
if (lenoffile>1){ // always fails
其中对0的任何此int检查都会导致函数返回而不执行预期的流。
private String readQuestions(String xml_file){
Log.i(TAG2,"readQuestions");
List<String> mapList = null;
String xmlString = null;
int lenoffile =0;
AssetManager am = this.getAssets();
try {
mapList = Arrays.asList(am.list(""));
if (mapList.contains(xml_file)){
InputStream is = am.open(xml_file);
int length = is.available();
byte[] data = new byte[length];
is.read(data);
xmlString = new String(data);
return xmlString;
}
} catch (IOException e1) {
Log.i(TAG,"Failed in ReadQuestions() from assets");
// e1.printStackTrace();
//return null;
}
Log.i(TAG2,"File not an asset. Must be on FS");
if (me.FileExists(Environment.getExternalStorageDirectory().getAbsolutePath() + APPDIR +"/"+xml_file))
xmlString = m.fileLoader(xml_file);
if (xmlString != null){
lenoffile = xmlString.length();
Log.i(TAG2,"Length of file found=" + lenoffile);
if (lenoffile>1){ // below never gets executed even with lenoffile==0
Log.i(TAG2,"Returning here with a file of length "+ lenoffile);
return xmlString; // assume the full file exists and validate
}
} // if else then there is a null situation
else{ // look on FS and load accordingly
DownloadFileAsync Z = new DownloadFileAsync();
迹:
06-09 12:26:58.197: I/SRCFILE(11792): readQuestions
06-09 12:27:08.822: I/SRCFILE(11792): File not an asset. Must be on FS
06-09 12:27:15.197: I/SRCFILE(11792): Length of file found=0
提前致谢。
凯文