简体   繁体   English

Android将字节数组转换为Bitmap的最佳方法?

[英]Android Best way to convert byte array to Bitmap?

I know why OutOfMemoryError Exception occurs.But there are any best way to convert byte array to Bitmap.And I used below code ,But when large byte it force close app and gives OutOfMemoryError Exception. 我知道为什么会出现OutOfMemoryError异常。但是有任何最好的方法将字节数组转换为Bitmap.And我使用下面的代码,但是当大字节时它强制关闭app并给出OutOfMemoryError Exception。

And i have API it just return me byte array nothing else. 我有API它只是返回字节数组没有别的。

Bitmap bmp = BitmapFactory.decodeByteArray(bytearray, 0, bytearray.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapbytes , 0, bitmapbytes .length);

返回已解码的位图,如果图像无法解码,则返回null。

Here is what worked for me: photo is a string of an image by the way. 这对我有用:照片是顺便说一下的一串图像。

byte[] imgbytes = Base64.decode(photo, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgbytes, 0,
imgbytes.length);
imageupload.setImageBitmap(bitmap);

You probably have to use this following method ( DOC , same method, but with the options parameter): 您可能必须使用以下方法( DOC ,相同的方法,但使用options参数):

public static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)

And play with the options parameter. 并使用options参数进行播放。 Hope this will help you =) 希望这会帮助你=)

You may want to use the AQuery library to load your images, this will help you resize, view etc and avoid the most common memory leaks. 您可能希望使用AQuery库来加载图像,这将帮助您调整大小,查看等,并避免最常见的内存泄漏。 This tool can be found here: http://code.google.com/p/android-query/ 可以在此处找到此工具: http//code.google.com/p/android-query/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM