如果要将图像缩小到特定尺寸,可以使用以下代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(LogoURL);
try {
    HttpResponse response = client.execute(request);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
        return null;
    }
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = url.openStream();
            bis = new BufferedInputStream(is);
            int sampleSize = 1;
            bis.mark(Integer.MAX_VALUE);
            Options bounds = new Options();
            bounds.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(bis, null, bounds);
            if (bounds.outWidth != -1) {
                int width = bounds.outWidth;
                int height = bounds.outHeight;
                boolean withinBounds = width <= YOUR_DESIRED_WIDTH && height <= YOUR_DESIRED_HEIGHT;
                int newWidth = width;
                int newHeight = height;
                while (!withinBounds) {
                    newWidth /= 2;
                    newHeight /= 2;
                    sampleSize *= 2;
                    withinBounds = newWidth <= YOUR_DESIRED_WIDTH && newHeight <= YOUR_DESIRED_HEIGHT;
                }
            } else {
                Log.w(LOG_TAG, "Can't open bitmap at " + url);
                return null;
            }
            try {
                bis.reset();
            } catch (IOException e) {
                if(is != null){
                    is.close();
                }
                if(bis != null){
                    bis.close();
                }
                if(!entity.isRepeatable()){
                    entity.consumeContent();
                    response = client.execute(request);
                    entity = response.getEntity();
                }
                is = entity.getContent();
                bis = new BufferedInputStream(is);
            }
            Options opts = new Options();
            opts.inSampleSize = sampleSize;
            Bitmap bm = BitmapFactory.decodeStream(bis, null, opts);
            return bm;
        } finally {
            if (is != null) {
                is.close();
            }               
            if (bis != null) {
                bis.close();
            }
            entity.consumeContent();
        }
    }
} catch (IOException e) {
    request.abort();
    Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
    request.abort();
    Log.w(LOG_TAG, "Incorrect URL: " + url);
} catch (Exception e) {
    request.abort();
    Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
}
用 打开图片时Options bounds = new Options(); bounds.inJustDecodeBounds = true;,不会下载图片数据,只下载图片的大小。我使用这个大小来计算新的比例以获得所需的宽度和高度。
使用该选项Options opts = new Options(); opts.inSampleSize = sampleSize;,BitmapFactory 将下载已调整大小的图像。这样可以节省内存和带宽。
请注意,sampleSize 值应该是 2 的幂。它也适用于不同的数字,但这更有效。