我有4行代碼下載一個位圖,的InputStream - 在什麼時候是數據下載
URL u = new URL(webaddress);
InputStream in = null;
in = u.openStream();
icon = BitmapFactory.decodeStream(in);
我打算將最後一行做類似這樣tutorial一些地方我只加載到記憶設置大小的圖像以減少內存使用量。不過,我不希望這涉及到另一個服務器調用/下載,所以我很好奇上面四行中的哪一行實際上從源下載數據?
即時將要改變的代碼的最後一行到上述教程中的最後兩個功能,所以可以做知道它是否意味着下載更多或更少的數據,(我試圖只下載一個小圖像從一個可能是例如500萬像素)
道歉,如果這是簡單的/錯誤的方式來思考它我不是很有經驗的數據流。
EDIT
使用這兩個功能,以取代上面的代碼的最後一行IM: 主叫:
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);
}
您可以將輸入流讀入緩衝區,然後從緩衝區創建輸入流 – Cruncher