将图片网址url转化为bitmap

2019-01-10 15:41:32 浏览数 (1)

代码语言:javascript复制
public Bitmap returnBitMap(final String url){

    new Thread(new Runnable() {
        @Override
        public void run() {
                  URL imageurl = null;

    try {
        imageurl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
        }
    }).start();

    return bitmap;
}
代码语言:javascript复制
//这里要注意的是
代码语言:javascript复制
要使用一个线程去访问,因为是网络请求

0 人点赞