首页 > 代码库 > Android 网络图片Url 转 Bitmap

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

 

Android 网络图片Url 转 Bitmap