0

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 stringis 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());
                              } 
Community
  • 1
  • 1
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
  • Refer to my answer here. [http://stackoverflow.com/questions/6909128/outofmemoryerror-when-opening-lot-of-little-pictures/6909204#6909204][1] [1]: http://stackoverflow.com/questions/6909128/outofmemoryerror-when-opening-lot-of-little-pictures/6909204#6909204 – Andro Selva Aug 02 '11 at 10:22
  • Thank you for your answer, I tried this, but got the same error... – Sandeep Kumar P K Aug 02 '11 at 10:53

1 Answers1

0

Use bm.recycle() and bm=null after each post to the server.

EDIT: I think it is useless to create a Bitmap for every file. You could just read the file into a StringBuffer and pass this to your JSONObject.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84