博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
listView simpleAdapter 加载网络图片
阅读量:4507 次
发布时间:2019-06-08

本文共 2532 字,大约阅读时间需要 8 分钟。

一、通过url地址获取Bitmap

 public Bitmap loadImage(String url) {

  final HttpClient client = new DefaultHttpClient();
  final HttpGet getRequest = new HttpGet(url);
  try {
   HttpResponse response = client.execute(getRequest);
   final int statusCode = response.getStatusLine().getStatusCode();
   if (statusCode != HttpStatus.SC_OK) {
    Log.w("TAG", "从" + url + "下载图片出处!,错误码:" + statusCode);
    return null;
   }
   final HttpEntity entity = response.getEntity();
   if (entity != null) {
    InputStream inputStream = null;
    try {
     inputStream = entity.getContent();
     final ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] b = new byte[1024];
     int read = -1;
     while ((read = inputStream.read(b)) != -1) {
      baos.write(b, 0, read);
     }

     final byte[] data = baos.toByteArray();

     baos.close();

     // 保持到sdcard

//      File imgFile = new File(Common.recommend_icon + "/" +
//      url.hashCode() + ".png");
//      if(!imgFile.exists()){imgFile.createNewFile();}
//      FileOutputStream fOut = new FileOutputStream(imgFile);
//      fOut.write(data);
//      fOut.close();

     final Bitmap bitmap = BitmapFactory.decodeByteArray(data,

       0, data.length);
     return bitmap;
    } finally {
     if (inputStream != null) {
      inputStream.close();
     }
     entity.consumeContent();
    }
   }
  } catch (IOException e) {
   getRequest.abort();
   Log.w("TAG", "I/O errorwhile retrieving bitmap from " + url, e);
  } catch (IllegalStateException e) {
   getRequest.abort();
   Log.w("TAG", "Incorrect URL:" + url);
  } catch (Exception e) {
   getRequest.abort();
   Log.w("TAG", "Error whileretrieving bitmap from " + url, e);
  }
  return null;
 }

二、将bitmap转为BitmapDrawable

  Resources resources=getResources();

      Bitmap bm=loadImage(imagePath);
      BitmapDrawable bd= new BitmapDrawable(resources, bm);

三、将BitmapDrawable对象放到simpleAdapter

     map = new HashMap<String, Object>();

     map.put("dd_refvalue", dd_refvalue);
     map.put("no_title", no_title);
     map.put("no_content", no_content);
     map.put("image", bd);
     map.put("more", "");
     list.add(map);

adapter = new SimpleAdapter(NoticeActivity.this, list,

      R.layout.notice_item, new String[] { "dd_refvalue",
        "no_title", "no_content", "image","more" }, new int[] {
        R.id.tvId, R.id.tvNoticeTitle, R.id.tvContent,
        R.id.image,R.id.tvMore});

四、为适配器setViewBinder

 adapter.setViewBinder(new ViewBinder(){

              public boolean setViewValue(View view,Object data,String textRepresentation){
                    if(view instanceof ImageView && data instanceof Drawable){
                    ImageView iv=(ImageView)view;
                         iv.setImageDrawable((Drawable)data);
                                   return true;
                              }
                                 else return false;
                              }
                         });

 

遇到问题:遇到地址为null的情况,需要令url=“”;

转载于:https://www.cnblogs.com/zhoujingjin/archive/2013/04/20/3032824.html

你可能感兴趣的文章
HackerRank "Training the army" - Max Flow
查看>>
Mesos源码分析(16): mesos-docker-executor的运行
查看>>
3771: Triple
查看>>
Python:yield关键字
查看>>
EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient
查看>>
EasyDarwin云存储方案调研:海康萤石云采用的是MPEG-PS打包的方式进行的存储
查看>>
MySQL巡检
查看>>
学习笔记之传说中的圣杯布局
查看>>
oh-my-zsh的使用
查看>>
共享内存的设计
查看>>
deque容器
查看>>
2017-2018-1 20155203 20155204 实验二 固件程序设计
查看>>
数据可视化视频制作
查看>>
mysql 数据备份。pymysql模块
查看>>
FactoryMethod模式——设计模式学习
查看>>
Android中 AsyncTask
查看>>
原码、反码、补码和移码
查看>>
SQL存储过程与函数的区别
查看>>
vue项目配置使用flow类型检查
查看>>
@Resource和@Autowired区别
查看>>