Android ImageView加载网络图片

获取网络图片地址,把返回图片转成Bitmap,然后用image View直接展示即可

    public Bitmap getImageBitmap() {
    
    
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
    
    
            imgUrl = new URL("传地址");
            HttpURLConnection conn = (HttpURLConnection) imgUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
          
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();

        } catch (MalformedURLException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return bitmap;
    }

展示图片

    image.setImageBitmap(bitmap);

猜你喜欢

转载自blog.csdn.net/Chen_xiaobao/article/details/125746743