Encoded byte64 image causes OutOfMemoryError android

*爱你&永不变心* 提交于 2019-12-23 04:58:03

问题


I'm getting quite crazy with this issue:

I am getting an OutOfMemoryError everytime I open my app. At some point, it loades an image from a server, but it's encoded y a Base64 string.

Whay I do is basically:

decodedString = Base64.decode(imagen_codificada);

 Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, 
                            decodedString.length); 

But i'm getting the damned OOME, everytime.

If tried with this:

BitmapFactory.Options options=new BitmapFactory.Options();
                        options.inSampleSize = 8;

Even setting options to 128, what is crazy, but still crashes...

Is there a way to reduce the size of the String codifying the image somehow before decoding it? I can't operate straightly with the image, because it's used for many other things withing the app and in desktop environments...

I attach the ERROR log:

12-21 10:36:27.032: E/dalvikvm-heap(5508): 10119168-byte external allocation too large for this process. 12-21 10:36:27.032: E/dalvikvm(5508): Out of memory: Heap Size=5823KB, Allocated=4351KB, Bitmap Size=720KB, Limit=13692KB 12-21 10:36:27.032: E/dalvikvm(5508): Trim info: Footprint=6343KB, Allowed Footprint=6343KB, Trimmed=520KB 12-21 10:36:27.032: E/GraphicsJNI(5508): VM won't let us allocate 10119168 bytes 12-21 10:36:27.192: E/AndroidRuntime(5508): FATAL EXCEPTION: main 12-21 10:36:27.192: E/AndroidRuntime(5508): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

EDIT: SOLVED WITH THIS:

decodedString = Base64.decode(imagen_codificada);
                    System.gc();
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    InputStream input = new ByteArrayInputStream(decodedString);
                    Bitmap decodedByte = BitmapFactory.decodeStream(input, null, 
                            options); 
                    menu.setNombreApellido(decodedByte);

Thanks to everyone!!


回答1:


try this
byte[] decodedString = (byte[])Base64.decode(imagen_codificada);
This will return u bit array directly . Now u can use
BitmapFactory.Options options=new BitmapFactory.Options(); // options.inSampleSize = 8; to scale down your photograph .



来源:https://stackoverflow.com/questions/8587787/encoded-byte64-image-causes-outofmemoryerror-android

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