如何将字节数组解析为Image(来自json的ByteArray响应)

时间:2015-07-01 09:25:59

标签: android

我从jsonresponse获得低于字符串,

String s="[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,117,0,0,0,117,8,6,0,0,0,112,4,174,188,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,-------------------------- ];
像这样,由于字符限制,我无法发布整个字符串,

try{
            byte[] a=s.getBytes();

            System.out.println("Size "+a.length);

            ImageView iv=(ImageView)findViewById(R.id.imageView1);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds=false;


            Bitmap decoded_photo_byte = BitmapFactory.decodeByteArray(a,0,a.length,options);

            System.out.println(" bitmap"+decoded_photo_byte);

            iv.setImageBitmap(decoded_photo_byte);

        }catch(Exception e){e.printStackTrace();}

但是图片没有显示,而是我得到07-01 14:35:44.181:D / skia(10239):--- SkImageDecoder :: Factory返回null

2 个答案:

答案 0 :(得分:0)

photodata是你从json收到的数据;

 try {
        byte[] data = android.util.Base64.decode(photodata, 0);

        Bitmap bmpNew = BitmapFactory.decodeByteArray(data, 0, data.length);
        profilepic.setImageBitmap(bmpNew);
    } catch (Exception e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

String s="[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,117,0,0,0,117,8,6,0,0,0,112,4,174,188,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,-------------------------- ]";

String[] s2 = s.substring( 1, s.length - 1 ).split( "," );
byte[] a = new byte[s2.length];
for( int i = 0; i < s2.length; ++i ) {
  a[i] = Byte.parseByte( s2[i] );
}