【问题标题】:Android Best way to convert byte array to Bitmap?Android 将字节数组转换为位图的最佳方法?
【发布时间】:2012-07-18 17:47:03
【问题描述】:

我知道为什么会发生 OutOfMemoryError 异常。但是有任何最好的方法可以将字节数组转换为位图。我使用了下面的代码,但是当大字节时它会强制关闭应用程序并给出 OutOfMemoryError 异常。

我有 API,它只返回字节数组。

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

【问题讨论】:

标签: java android byte


【解决方案1】:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapbytes , 0, bitmapbytes .length);

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

【讨论】:

    【解决方案2】:

    这对我有用: 顺便说一下照片是一串图片。

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

    【讨论】:

      【解决方案3】:

      您可能必须使用以下方法(DOC,相同的方法,但带有 options 参数):

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

      并使用options 参数。希望这对你有帮助 =)

      【讨论】:

        【解决方案4】:

        您可能希望使用 AQuery 库来加载图像,这将帮助您调整大小、查看等并避免最常见的内存泄漏。这个工具可以在这里找到:http://code.google.com/p/android-query/

        【讨论】:

        • 如何使用 AQuery 将 byte[] 转换为位图?
        猜你喜欢
        • 2012-11-09
        • 2018-05-06
        • 1970-01-01
        • 2013-06-25
        • 2023-04-06
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多