将流转换为BitMap对象

从网络上获取一个图片地址,将这个图片通过流的方式转化成bitMap对象

try {
    //得到一个URL对象,u是传入的图片地址
            URL url = new URL(u);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(3000);
            urlConnection.setConnectTimeout(3000);
            if (urlConnection.getResponseCode() == 200) {
                InputStream inputStream = urlConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

通过URLConnection得到字节流,再通过BitmapFactory.decodeStream这个方法将流转化为bitmap对象

你可能感兴趣的:(将流转换为BitMap对象)