处理多于一张图像时发生内存不足错误

发布于 2024-11-27 14:10:50 字数 2337 浏览 0 评论 0原文

可能的重复:
OutOfMemoryError:位图大小超出虚拟机预算:- Android

我有类似的代码以下,捕获的图像大小约为 300KB。如果imageCount = 1它工作正常,这意味着它可以处理一张图像而不会出现内存异常。但是当我们有多个图像时,它会产生java.lang.OutOfMemoryError:位图大小超出VM预算。我认为问题是,在每次迭代for循环之后,为位图、字节数组和字符串分配的空间都没有释放。我尝试了 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());
                              } 

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());
                              } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-12-04 14:10:50

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文