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

扶醉桌前 提交于 2019-12-02 21:00:51

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.

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!