android从url加载采样位图

时间:2013-03-20 12:43:39

标签: android bitmap memory-efficient

我试图从网址有效地加载位图,但不知何故最终我得到了空的imageview。如果从资源解码,一切都在工作。

try {
    InputStream in = new java.net.URL(urldisplay).openStream();
    mIcon11 = decodeSampledBitmapFromStream(in, 200, 200);
}
protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}

public static 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;
    BitmapFactory.decodeStream(in, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(in, null, options);
}

0 个答案:

没有答案