根据Json byte[]值在imageView中显示图像,SkImageDecoder::Factory返回null



我正在尝试将byte[]转换为图像。byte[]来自JSON,其格式如下:

json:的字节数组值

"255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132,0,9,6,7,20,19,18,20,20,19,20,21,22,20,22,20,21,...".

我使用以下代码将byte[]转换为位图图像。

            String encodedString="255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0....";
            byte[] encodeByte = encodedString.getBytes();
            System.out.println("encodeByte : "+encodeByte);
            System.out.println("encodeByte.length : "+encodeByte.length);
            Bitmap photo=BitmapFactory.decodeByteArray(encodeByte, 0,                     encodeByte.length);
            System.out.println("Bitmap photo : "+photo);
            btnImg.setImageBitmap(photo);

我在Logcat中得到以下错误:

10-31 00:20:57.210: I/System.out(1315): encodeByte : [B@b3d249d8
10-31 00:20:57.210: I/System.out(1315): encodeByte.length : 18320
10-31 00:20:57.220: D/skia(1315): --- SkImageDecoder::Factory returned null
10-31 00:20:57.220: I/System.out(1315): Bitmap photo : null

我遇到了这样的错误。请帮我解决

这就是将byteArray转换为Base64Encoded String并将其转换为位图图像的方法。

使用以下代码将字节[]转换为位图图像。

//将byteArray转换为base64字符串

            byte[] photo_byte = (your byte[] from json response);
            //To encode byte[] as string for other purpose
            String photoString =Base64.encodeToString(photo_byte, Base64.DEFAULT);
            //decode the string to bitmap 
            byte[] decodedString = Base64.decode(photoString, Base64.DEFAULT);
            Bitmap decoded_photo_byte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

最新更新