Android 【将图片网址Url转化为Bitmap工具类】

2021-04-07 14:55:30 浏览数 (1)

需求将图片的url转为Bitmap格式

代码如下

代码语言:javascript复制
public class BitMapUtil {
   

    private Bitmap bitmap;

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

0 人点赞