2016-05-17 80 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更多信息