网络获取图片



 private Bitmap bitmap;



   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_glide);
        img = findViewById(R.id.iamge);
        img2 = findViewById(R.id.iamge2);
        img3 = findViewById(R.id.image3);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    httpUrlConnectionImage();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
}

    android.os.Handler handler = new android.os.Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            img.setImageBitmap(bitmap);
        }
    };

 private void httpUrlConnectionImage() throws MalformedURLException {
        URL url = new URL("http://img.my.csdn.net/uploads/201407/26/1406383291_6518.jpg");
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            handler.sendEmptyMessage(2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容