网络请求的图片展示到ImageView上

new Thread ( new Runnable ( ) {
                      @Override
                      public void run() {
                          try {
                              URL url=new URL ( "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2877661996,3369303060&fm=26&gp=0.jpg" );
                              //创建连接方法
                              HttpURLConnection connection = (HttpURLConnection) url.openConnection ( );
                              //连接时间
                              connection.setConnectTimeout ( 10000 );
                              //请求方式
                              connection.setRequestMethod ( "GET" );
                              //接收语言
                              connection.setRequestProperty ( "Accept-Language", "zh-CN,zh;q=0.9" );//可能错
                              //这里是设置压缩
    //                    connection.setRequestProperty ( "Accept-Encoding","gzip,deflate" );
                              //接收范围:这里是全部
                              connection.setRequestProperty ( "Accept", "*/*" );
                              //设置连接
                              connection.connect ( );
                              //返回结果码
                              final int responseCode = connection.getResponseCode ( );
                              if (responseCode==HttpURLConnection.HTTP_OK){
                                  final InputStream inputStream = connection.getInputStream ( );//得到数据流
                                  final Bitmap bitmap = BitmapFactory.decodeStream ( inputStream );//将流转为bitmap
                                  result_image.setImageBitmap ( bitmap );//建立图片
                              }else {
                                  Log.d ( TAG,"抱歉,出错了,帅哥");
                              }
                          } catch (Exception e) {
                              e.printStackTrace ( );
                          }
                      }
                  } ).start ();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值