线程URL访问网络资源

Bitmap bitmap;
//全局变量
Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what ==  123) {
                // 将获取到的图片显示到控件上面
                 imageView0.setImageBitmap(bitmap);
            }

        };
    };


// 开启线程
        new Thread() {
            public void run() {
                try {
                    // 实例化URL对象
                    URL url = new URL(
                //计算机本地 IP地址 ipconfig查看本机IP地址
                            " http://192.168.0.102:8080/Study/image/a.jpg");
                    // 建立链接
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    // 获取输入流对象
                    InputStream is = conn.getInputStream();
                    // 将流转化成Bitmap对象
                    bitmap = BitmapFactory.decodeStream(is);
                    // 发送一个空消息
                    handler.sendEmptyMessage( 123);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();

----------------------------------------------------------------------------------------------------------------
访问网页跳转到浏览器:
new Thread() {
            public void run() {
                try {
                    Uri uri = Uri.parse(" http://192.168.43.74:50551/");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //handler.sendEmptyMessage(0);
            };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值