【发布时间】:2011-10-18 02:29:55
【问题描述】:
可能重复:
OutOfMemoryError: bitmap size exceeds VM budget :- Android
我有如下代码,捕获的图像大小约为 300KB。如果imageCount = 1 工作正常,这意味着它可以处理一张图像而不会出现内存异常。但是当我们有多个图像时,它会产生java.lang.OutOfMemoryError:bitmap size exceeds VM budget。我认为问题是,在for loop 的每次迭代之后,为bitmap,byte array and string 分配的空间不会释放。我试过 System.gc(),没用。我该如何解决????
try{
HttpPost post = new HttpPost("http:url");
for( int i = 0; i <= imageCount; i++ ) {
String methodName = "new_receipt";
JSONObject json = new JSONObject();
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "captured_receipt"+i+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //6291456 9555000
bm.compress(Bitmap.CompressFormat.JPEG, 10, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeBytes(b);//Base64.DEFAULT
json.put("Key", key);
json.put("image_no", i);
json.put("image",encodedImage);
methodName = "add_image";
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("module", "expense"));
nameValuePairs.add(new BasicNameValuePair("method", methodName));
nameValuePairs.add(new BasicNameValuePair("json", json.toString()));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = client.execute(post);
}
} catch(Exception e){
Log.d("MY Error",e.getMessage());
}
【问题讨论】:
-
谢谢你的回答,我试过这个,但得到了同样的错误......
标签: android memory-leaks out-of-memory