2012-06-27 212 views
0

好吧,所以我下載一個圖像,併發送輸入流到這個方法巫婆必須解碼一次找到圖像大小,然後計算一個比例值,然後從流中創建一個迷你版本的位圖。 ..但我得到的logcat錯誤,bitmapFactory返回null,任何人都有一個想法可能是錯誤的?BitmapFactory返回null Android

public static Bitmap getSampleBitmapFromStream(InputStream is, 
     int reqWidth, int reqHeight) { 

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

    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(new FlushedInputStream(is), null, options); 


    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = options.outWidth, height_tmp = options.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp/2 < reqWidth || height_tmp/2 < reqHeight) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // decode with inSampleSize 
    options.inJustDecodeBounds = false; 
      options.inSampleSize = scale; 
    return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options); 

} 

回答

1

的原因是:您使用的流兩次

options.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options); 

,並再次:

options.inJustDecodeBounds = false; 
     options.inSampleSize = scale; 
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options); 

所以如果你想避免這種情況,你需要關閉流和重新打開流。

0

如何直接提供InputStream?

return BitmapFactory.decodeStream(is, null, options);