解决方案 »

  1.   

    根据url获得图片来源(这段代码写在子线程里)HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.connect();
    InputStream input = conn.getInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(input);
    input.close();
    conn.disconnect();
    return bitmap;
    /**
     * 压缩图片
     * 
     * @param bitmap
     *            源图片
     * @param width
     *            想要的宽度
     * @param height
     *            想要的高度
     * @param isAdjust
     *            是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩
     * @return Bitmap
     */
    public Bitmap reduce(Bitmap bitmap, int width, int height, boolean isAdjust) {
    if (null == bitmap) {
    return null;
    }
    if (bitmap.getWidth() < width && bitmap.getHeight() < height && isAdjust) {
    return bitmap;
    }
    float sx = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN)
    .floatValue();
    float sy = new BigDecimal(height).divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN)
    .floatValue();
    if (isAdjust) {
    sx = (sx < sy ? sx : sy);
    sy = sx;
    }
    Matrix matrix = new Matrix();
    matrix.postScale(sx, sy);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }