0

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);
  }
 }

}

U Avalos
  • 5,553
  • 6
  • 38
  • 67

1 Answers1

0

This technique is not optimized for Android and will likely run poorly. The convention is to use AndroidHttpClient:

Subclass of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes.

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 the InputStream with the FileInputStream from your snippet. Converting the InputStream to 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 of OutOfMemoryErrors, so take care that you don't read files that are too large (I would suggest less than 1 MB).

public void executeMultipartPost() throws Exception {
    try {
        InputStream is = this.getAssets().open("data.xml");
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://w3mentor.com/Upload.aspx");
        byte[] data = IOUtils.toByteArray(is);
        InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data),"uploadedFile");
        StringBody sb1 = new StringBody("someTextGoesHere");
        StringBody sb2 = new StringBody("someTextGoesHere too");
        MultipartEntity multipartContent = new MultipartEntity();
        multipartContent.addPart("uploadedFile", isb);
        multipartContent.addPart("one", sb1);
        multipartContent.addPart("two", sb2);
        postRequest.setEntity(multipartContent);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();
    } catch (Throwable e) {
        // handle exception here
    }
}
Community
  • 1
  • 1
Paul Lammertsma
  • 35,234
  • 14
  • 128
  • 182
  • I think you misunderstood me. I'm loading the Bitmap from an asset, not over the Internet. And there should be enough space. It's just a small bitmap. Although checking the memory is always a good idea – U Avalos Jun 04 '11 at 15:36
  • I've added an example; hope this helps. – Paul Lammertsma Jun 05 '11 at 17:44
  • I twice overlooked the fact that you're not trying to grab it from the internet. Unfortunately, I don't have any recommendations for loading assets through an InputStream. I would suspect that Android's technique is optimized, however. – Paul Lammertsma Jul 07 '11 at 23:34