As per this link below:
http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/
You can speed up loading of bitmaps (or any files) if you do the buffering yourself (i.e., instead of using BufferedInputStream, you handle the buffering yourself).
In particular, Approach 4 looks promising (slurp whole file at a time). However, I have no idea how to implement that in android. Here’s the Java code:
import java.io.*;
public class readfile {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("missing filename");
System.exit(1);
}
try {
int len = (int)(new File(args[0]).length());
FileInputStream fis =
new FileInputStream(args[0]);
byte buf[] = new byte[len];
fis.read(buf);
fis.close();
int cnt = 0;
for (int i = 0; i < len; i++) {
if (buf[i] == '\n')
cnt++;
}
System.out.println(cnt);
}
catch (IOException e) {
System.err.println(e);
}
}
}
This technique is not optimized for Android and will likely run poorly. The convention is to use AndroidHttpClient:
If you really want to use Sun’s code above, you should be careful because you will likely exceed the VM heap budget when the size of the file exceeds the amount of heap space available to the application.
It would be wise to first check if there is sufficient heap space left using ActivityManager. See also the elaborate answer to this question.
Edit:
I’ve found an example of sending an InputStream via POST. Here a file is being read from a resource (
res/data.xml), but you could replace theInputStreamwith theFileInputStreamfrom your snippet. Converting theInputStreamto a byte array does essentially the same as your code: read the entire file into memory and push it into the request. This is a notorious cause ofOutOfMemoryErrors, so take care that you don’t read files that are too large (I would suggest less than 1 MB).