网络图片转换为Bitmap

2016-04-05  本文已影响3125人  jsnow0613
/**
 * 网络图片转换为Bitmap
 *
 * @Author: ChengBin
 * @Time: 16/4/5 下午2:41
 */
public static Bitmap netPicToBmp(String src) {
    try {
        Log.d("FileUtil", src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);

        //设置固定大小
        //需要的大小
        float newWidth = 200f;
        float newHeigth = 200f;

        //图片大小
        int width = myBitmap.getWidth();
        int height = myBitmap.getHeight();

        //缩放比例
        float scaleWidth = newWidth / width;
        float scaleHeigth = newHeigth / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeigth);

        Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, width, height, matrix, true);
        return bitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}
上一篇下一篇

猜你喜欢

热点阅读