免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2621 | 回复: 1
打印 上一主题 下一主题

android listview 滚动时异步加载图片的问题接上 (3)———————— [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-05 16:02 |只看该作者 |倒序浏览
android listview 滚动时异步加载图片的问题 接上(3)..............................
......
  1. package com.gowin.cach;

  2. import java.io.FilterInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpGet;

  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.net.http.AndroidHttpClient;
  13. import android.util.Log;

  14. public class ImageGetForHttp {
  15.     private static final String LOG_TAG="ImageGetForHttp";
  16.      public static Bitmap downloadBitmap(String url) {
  17.             //final int IO_BUFFER_SIZE = 4 * 1024;

  18.             // AndroidHttpClient is not allowed to be used from the main thread
  19.             final HttpClient client = AndroidHttpClient.newInstance("Android");
  20.             final HttpGet getRequest = new HttpGet(url);

  21.             try {
  22.                 HttpResponse response = client.execute(getRequest);
  23.                 final int statusCode = response.getStatusLine().getStatusCode();
  24.                 if (statusCode != HttpStatus.SC_OK) {
  25.                     Log.w("ImageDownloader", "Error " + statusCode +
  26.                             " while retrieving bitmap from " + url);
  27.                     return null;
  28.                 }

  29.                 final HttpEntity entity = response.getEntity();
  30.                 if (entity != null) {
  31.                     InputStream inputStream = null;
  32.                     try {
  33.                         inputStream = entity.getContent();
  34.                         // return BitmapFactory.decodeStream(inputStream);
  35.                         // Bug on slow connections, fixed in future release.
  36.                         FilterInputStream fit= new FlushedInputStream(inputStream);
  37.                         return BitmapFactory.decodeStream(fit);
  38.                     } finally {
  39.                         if (inputStream != null) {
  40.                             inputStream.close();
  41.                         }
  42.                         entity.consumeContent();
  43.                     }
  44.                 }
  45.             } catch (IOException e) {
  46.                 getRequest.abort();
  47.                 Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
  48.             } catch (IllegalStateException e) {
  49.                 getRequest.abort();
  50.                 Log.w(LOG_TAG, "Incorrect URL: " + url);
  51.             } catch (Exception e) {
  52.                 getRequest.abort();
  53.                 Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
  54.             } finally {
  55.                 if ((client instanceof AndroidHttpClient)) {
  56.                     ((AndroidHttpClient) client).close();
  57.                 }
  58.             }
  59.             return null;
  60.         }
  61.      
  62.      /*
  63.          * An InputStream that skips the exact number of bytes provided, unless it reaches EOF.
  64.          */
  65.         static class FlushedInputStream extends FilterInputStream {
  66.             public FlushedInputStream(InputStream inputStream) {
  67.                 super(inputStream);
  68.             }

  69.             @Override
  70.             public long skip(long n) throws IOException {
  71.                 long totalBytesSkipped = 0L;
  72.                 while (totalBytesSkipped < n) {
  73.                     long bytesSkipped = in.skip(n - totalBytesSkipped);
  74.                     if (bytesSkipped == 0L) {
  75.                         int b = read();
  76.                         if (b < 0) {
  77.                             break;  // we reached EOF
  78.                         } else {
  79.                             bytesSkipped = 1; // we read one byte
  80.                         }
  81.                     }
  82.                     totalBytesSkipped += bytesSkipped;
  83.                 }
  84.                 return totalBytesSkipped;
  85.             }
  86.         }


  87. }
复制代码
使用时,要救imageview的tag是需要下载的url

在getview时先addtask

当滚动停止的时候调用doTask

论坛徽章:
0
2 [报告]
发表于 2011-12-23 23:15 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP