我有一个函数可以查看 android 资产并在资产存在时加载。如果不是,则查看文件系统。如果找到它,它会加载它 - 如果文件不是零字节。如果在文件系统上找不到它,它会尝试 2 次从服务器获取它。所有这些都有效,除非文件已经存在并且大小为 0 - 并且它在某些“原始”java 逻辑测试中失败。我看不出这里有什么问题,并探索它是我自己代码之外的错误的可能性。
问题发生在
if (lenoffile>1){ // always fails
对此 int 与 0 的任何检查都会导致函数返回并且不执行预期的流程。
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
提前致谢。
凯文