【问题标题】:Out of memory error occurred when handling more than One image [duplicate]处理多个图像时发生内存不足错误[重复]
【发布时间】: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


【解决方案1】:

在每次发布到服务器后使用bm.recycle() 和bm=null。

编辑:我认为为每个文件创建一个Bitmap 是没有用的。您可以将文件读入StringBuffer 并将其传递给您的JSONObject。

【讨论】:

  • 感谢您的回答,遗憾的是没有任何改变。我该怎么办??
  • 我用bm.recycle()和bm = null,把JSON换成文本文件,就可以了!!!
猜你喜欢
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2020-06-09
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
相关资源
最近更新 更多