五种网络图片加载

HttpURLConnection方式:

public Bitmap getImageBitmap(String url) {
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imgUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
}

HttpClient方式
public Bitmap getImageBitmap(String url) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        try {
            HttpResponse resp = httpclient.execute(httpget);
            // 判断是否正确执行
            if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
                // 将返回内容转换为bitmap
                HttpEntity entity = resp.getEntity();
                InputStream in = entity.getContent();
                Bitmap mBitmap = BitmapFactory.decodeStream(in);
                // 向handler发送消息,执行显示图片操作
                return mBitmap;
            }
 
        } catch (Exception e) {
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
 
        return null;
}

 XUtils方式
private void initView() {
    // TODO Auto-generated method stub
    BitmapUtils bitmapUtils = new BitmapUtils(this);
    // 加载网络图片
    bitmapUtils.display(imageView,
            "https://img-my.csdn.net/uploads/201407/26/1406383290_9329.jpg");
 
    // 加载本地图片(路径以/开头, 绝对路径)
    // bitmapUtils.display(imageView, "/sdcard/test.jpg");
 
    // 加载assets中的图片(路径以assets开头)
    // bitmapUtils.display(imageView, "assets/img/wallpaper.jpg");
 
  }

OkHttp方式
  private void setIamge()
    {
        String url = "https://img-my.csdn.net/uploads/201407/26/1406383291_8239.jpg";
        OkHttpUtils.get().url(url).tag(this)
                .build()
                .connTimeOut(20000).readTimeOut(20000).writeTimeOut(20000)
                .execute(new BitmapCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    }
 
                @Override
                public void onResponse(Bitmap bitmap, int id) {
                        imageView.setImageBitmap(bitmap);
                    }
                });
    }

Volley方式

 /***
     * ImageRequest加载图片
     */
 public void setImg1()
 {
        
        
    ImageRequest request = new ImageRequest(VolleySingleton.imageThumbUrls[0],
         new Response.Listener<Bitmap>() {
         @Override
         public void onResponse(Bitmap bitmap) {
                imageview1.setImageBitmap(bitmap);
               }
         }, 0, 0, Config.RGB_565,
         new Response.ErrorListener() {
             public void onErrorResponse(VolleyError error) {
                imageview1.setImageResource(R.mipmap.ic_launcher);
              }
    });
      VolleySingleton.getVolleySingleton(this.getApplicationContext()).addToRequestQueue(request);
    }
    /***
     * 使用 ImageLoader 加载图片
     */
    
    public void setImg2()
    {
        com.android.volley.toolbox.ImageLoader mImageLoader;
        mImageLoader =   VolleySingleton.getVolleySingleton(this.getApplicationContext()).getImageLoader();
        mImageLoader.get(VolleySingleton.imageThumbUrls[1], 
                //mImageView是ImageView实例
                //第2个参数:默认图片
                //第2个参数:加载图片错误时的图片
        com.android.volley.toolbox.ImageLoader.getImageListener(imageview2,R.mipmap.ic_launcher, R.mipmap.ic_launcher));
    }
    /**
     * 使用NetworkImageView加载图片
     */
    public void setImg3()
    {
        com.android.volley.toolbox.ImageLoader mImageLoader;
        mImageLoader = VolleySingleton.getVolleySingleton(this.getApplicationContext()).getImageLoader();
        networkImageView.setImageUrl(VolleySingleton.imageThumbUrls[2], mImageLoader);
    }



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值