2012-06-27 67 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);