处理多于一张图像时发生内存不足错误
我有类似的代码以下,捕获的图像大小约为 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 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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次发布到服务器后使用
bm.recycle()
和bm=null
。编辑:我认为为每个文件创建一个位图是没有用的。您只需将文件读入
StringBuffer
并将其传递给您的JSONObject
。Use
bm.recycle()
andbm=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 aStringBuffer
and pass this to yourJSONObject
.