开发者

Faster Loading of Bitmaps

开发者 https://www.devze.com 2023-03-10 21:48 出处:网络
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

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:

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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号