Out of memory on a byte allocation (Bitmap as String to webservice using soap)
Asked Answered
W

3

11

Am having a bitmap , so I want to upload a webserivceas string and want to retrive the string.

For converting bitmap to string am using:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

strBase64 = Base64.encodeToString(byteArray, Base64.URL_SAFE);

this above String is using as property to in soapobject to upload.

But am getting Out of memory on a 11674900-byte allocation, while print and uploading.

And if i debugged the issue, without printing am getting

com.sun.jdi.InvocationException occurred invoking method.

on soaprequest.

How to resolve this issue and to upload image to webservice as string ?

Whodunit answered 10/11, 2011 at 11:46 Comment(0)
B
9

You are creating 3 copies of an 11MB image(bitmap, stream, strBase64). So reduce the memory usage by calling

bitmap.recycle();

below this line:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

Also close the stream when you are done with it(below stream.toByteArray();):

stream.close();
stream = null;

Remember that there is no guarantee that memory will be cleaned immediately after these calls. Proper way to handle this type of situation is to transfer large files chunk by chunk.

Borak answered 10/11, 2011 at 11:52 Comment(2)
it solved by bytes array and have used serialization before soap request.Whodunit
@Udayakiran,I am having same error , what you mentioned in question.So please tell the way you fixed this issue.Overissue
A
0

An 11 Million byte allocation much larger than most phones' heap can handle. you definitely don't want to be holding a byte array of that size in memory.

Try using insample size with

BitmapFactory.decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)

and settings options to use insample size to return a reasonably sized image.

Alecto answered 13/4, 2012 at 19:13 Comment(2)
Are you saying storing 11mb of data in memory is not possible on android phones?Tamra
It might be possible, but either your phone does not have that much free memory left or it cannot deallocate memory as fast as your are allocating it.Alecto
G
-1

A simple fix for some might be to add android:configChanges="orientation|screenSize" to your manifest. In my case, the Nexus_S emulator was crashing without this line, while the actual Nexus 7 device I was testing on wasn't crashing on rotation.

Adding this appears to be an easy fix for apps that have a couple large "match_parent" bitmaps to rotate and resize.

Careful if you are building for APIs before 13!

Guinn answered 8/4, 2013 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.