通过图片url显示图片在imageview上

 public Bitmap getBitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }


     //或者是通过glide框架
Glide.with(this).load(url).bitmapTransform(
                new GlideRoundTransform(this, 2)).placeholder(R.drawable.img_default)
                .error(R.drawable.img_default
).into(mIvView);


发布了4 篇原创文章 · 获赞 1 · 访问量 1749

猜你喜欢

转载自blog.csdn.net/liliKing1125/article/details/79541525