Android 网络图片Url 转 Bitmap

注意:该方法必须要在子线程中调用,因为涉及网络请求

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;
    }

 

posted @ 2017-04-28 00:33  星辰之力  阅读(6892)  评论(0编辑  收藏  举报