根据url获取Bitmap对象

根据url获取Bitmap对象

代码如下:

public Bitmap getBitmapByURL(String urlStr){
    Bitmap bitmap;
    InputStream is = null;
    try{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        is = new BufferedInputStream(conn.getInputStream());
        bitmap = BitmapFactory.decodeStream(is);
        conn.disconnect();//断开连接
        return bitmap;      
    }catch(Exception e){
    }finally{
        try{
            is.close();//关闭流
        }catch(Exception e){
        }
    }
    return null;
}

你可能感兴趣的:(Android)