Breaking News
Home / Android / Android Issues / Photo Upload in Android – Out of memory

Photo Upload in Android – Out of memory

Issue

Use Case

Trying to upload an Image from the Android Phone to a remote service by reading the file bytes and converting it into Base64 Encoded String. This generally can happen when the size of image to upload is large say several MB and there is not enough heap size allocated for the app by the Android Runtime System.

Error

11-24 21:38:59.597: E/dalvikvm(17341): Out of memory: Heap Size=65571KB, Allocated=62301KB, Limit=65536KB
11-24 21:38:59.597: E/dalvikvm(17341): Extra info: Footprint=65443KB, Allowed Footprint=65571KB, Trimmed=1684KB
11-24 21:38:59.597: W/System.err(17341): java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=62301KB)
11-24 21:38:59.597: W/System.err(17341):      at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
11-24 21:38:59.597: W/System.err(17341):      at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
11-24 21:38:59.597: W/System.err(17341):      at android.graphics.Bitmap.nativeCompress(Native Method)
11-24 21:38:59.597: W/System.err(17341):      at android.graphics.Bitmap.compress(Bitmap.java:1046)
11-24 21:38:59.597: W/System.err(17341):      at com.service.activity.UploadPhotoScreen.onActivityResult(UploadPhotoScreen.java:147)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
11-24 21:38:59.607: W/System.err(17341):      at android.os.Handler.dispatchMessage(Handler.java:99)<!>

Possible Solutions

– Reduce the size of the image file by changing the compression to JPG instead of PNG. JPG quality can be lower but helps in size reduction.

– Use Thumbnail or Corp section of image instead of the whole image as needed.

– Review code to make sure the image is loaded once in memory, remember String are immutable so make sure you have one copy of Base64 encoded String

– Use low level API to copy bytes in buffer incrementally and flush it out as well to the remote service in chunks.

[pb_builder]

Check Also

Cannot launch Call Intent – Android Permission Denial Issue

Issue When trying to invoke the Phone Dialer programmatically the following exception occurs. 10-24 11:37:41.127: …

Advertisment ad adsense adlogger