Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android
I have code like following, and captured image has size around 300KB. If the imageCount = 1
it works properly that means it can handle one image without memory exception. But when we have more than one image, it produces java.lang.OutOfMemoryError:bitmap size exceeds VM budget
. I think the problem is, after each iteration of for loop
the allocated space for bitmap,byte array and string
is do not make free. I tried System.gc(), no use. How can i solve this????
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());
}