Android Best way to convert byte array to Bitmap?

前端 未结 4 1773
挽巷
挽巷 2021-01-04 12:49

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 giv

相关标签:
4条回答
  • 2021-01-04 13:33

    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. This tool can be found here: http://code.google.com/p/android-query/

    0 讨论(0)
  • 2021-01-04 13:43
    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapbytes , 0, bitmapbytes .length);
    

    Returns The decoded bitmap, or null if the image could not be decode.

    0 讨论(0)
  • 2021-01-04 13:52

    You probably have to use this following method (DOC, same method, but with the options parameter):

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

    And play with the options parameter. Hope this will help you =)

    0 讨论(0)
  • 2021-01-04 13:56

    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);
    
    0 讨论(0)
提交回复
热议问题