美文网首页
网络图片转换为Bitmap

网络图片转换为Bitmap

作者: jsnow0613 | 来源:发表于2016-04-05 19:17 被阅读3125次
    /**
     * 网络图片转换为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;
        }
    }

    相关文章

      网友评论

          本文标题:网络图片转换为Bitmap

          本文链接:https://www.haomeiwen.com/subject/eqbplttx.html