【发布时间】:2015-07-02 12:26:53
【问题描述】:
我从互联网上下载了一张大图,并尝试使用以下方法对其进行缩放。问题是返回的位图为空。有没有不同的方法可以做到这一点,或者我在做不必要的事情。
public static Bitmap decodeBitmapFromInputStream(InputStream inputStream,
int reqWidth, int reqHeight) {
BufferedInputStream bis = new BufferedInputStream(inputStream);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bis, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeStream(bis, null, options);
return b;
}
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;
}
【问题讨论】:
-
尝试使用 volley 或 picasso 库、square.github.io/picasso、developer.android.com/training/volley/request.html