我有 4 行代码来下载位图,
URL u = new URL(webaddress);
InputStream in = null;
in = u.openStream();
icon = BitmapFactory.decodeStream(in);
我计划更改最后一行以执行类似于本教程的操作,我只将设置大小的图像加载到内存中以减少内存使用量。但是我不希望这涉及另一个服务器调用/下载,所以我很好奇上面四行中的哪一行实际上从源下载数据?
我将把最后一行代码更改为上面提到的教程中的最后两个函数,这样可以知道它是否意味着下载更多或更少的数据,(我试图只从一个例如可以是 5 兆像素)
抱歉,如果这是简单的/错误的思考方式,我对数据流不是很有经验。
编辑
我使用这两个函数来替换上面的最后一行代码:调用:
image = decodeSampledBitmapFromStram(in, 300,300);
图像质量不是优先事项,这是否意味着下载更多数据?
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
private Bitmap decodeSampledBitmapFromStream(InputStream in, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Rect padding = new Rect();
BitmapFactory.decodeStream(in, padding, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(in, padding, options);
}