Sunday 29 January 2012

Endianness issues in Java

Java is big endian. C endianness depends on CPU. In iphone simulator on x86 based Mac, C is little endian. Jave has problem loading a binary file saved by an iphone app run in iphone simulator. When saving the binary file to be opened by a Java program, each variable needs to reverse the byte order.

Android: cannot load files larger than 1MB

Android cannot load files larger than 1MB under assets or res/raw.

This is a known issue:
http://stackoverflow.com/questions/2860157/load-files-bigger-than-1m-from-assets-folder

The workaround is to rename the file as .png so that android won't zip it.

Monday 16 January 2012

Android problem with ArrayList of String

I was trying to read a text file to an ArrayList of String. The code is like this:

public class SymptomCategory {
private ArrayList<String> mCategory;
public SymptomCategory (InputStream is) {
String str="";
StringBuffer buf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
try {
while ((str = reader.readLine()) != null) {
System.out.println(str);
mCategory.add(new String(str));
buf.append(str + "\n" );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ArrayList<String> getCategory() {
return mCategory;
}
}

The problem was that I can only read the first line of the file.

I fixed the problem by adding the following at the beginning of the constructor:

mCategory = new ArrayList<String>();

Sunday 15 January 2012

Android problem: cannot find text file under assets


I put symp.txt under assets and use the following code to load it:

    AssetManager am = context.getAssets();
    InputStream is;
try {
    is = am.open("symp.txt");
    mCategory = new SymptomCategory(is);
} catch (IOException e) {
    e.printStackTrace();
}

However I got exception when opening the file.

The problem was fixed by cleaning up the project and let it auto recompile.