2016-05-17 79 views
0

我遇到了问题。我不明白,如何从下面的json响应将图像解码为位图。Android将图像解码为位图中的字节数组

response: [{"namae":"example", 
    photo:[125,122,10,22,34,5,5,56,5,0,0,..........................,23]}] 

我不知道如何从照片参数值获取位图图像。

在此先感谢。

回答

0

使用decodeByteArray()BitmapFactory

0

获取从JSON的字节数,让叫它

bytes photoBytes=array.getJSONArray(0); 

这样做:

BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length,options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, <preferredWidth>, <preferredHeight>); 
     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmp1=BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length,options); 

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
// Raw height and width of image 
final int height = options.outHeight; 
final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 

    final int halfHeight = height/2; 
    final int halfWidth = width/2; 

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
    // height and width larger than the requested height and width. 
    while ((halfHeight/inSampleSize) > reqHeight 
      && (halfWidth/inSampleSize) > reqWidth) { 
     inSampleSize *= 2; 
    } 
} 

return inSampleSize;} 

检查google site更多信息