[转]Android底下多线程下载远程图片

本帖最后由 linux初学三月 于 2010-07-23 11:41 编辑

在某些时候我们需要在Android设备下载远端服务器上的图片来进行显示,这里我整理出两种比较好的方法来实现远程图片的下载。

  方法一、直接通过Android官方提供的Http类访来问远程服务器,这里AndroidHttpClient是SDK 2.2中新出的方法,API Level为8,大家需要注意下,静态访问可以直接调用,如果SDK版本较低可以考虑Apache的Http库,当然HttpURLConnection或URLConnection也可以。

  1. static Bitmap downloadBitmapByCwj(String url) {
  2.     final AndroidHttpClient client = AndroidHttpClient.newInstance("linux初学三月");
  3.     final HttpGet getRequest = new HttpGet(url);
  4.     try {
  5.         HttpResponse response = client.execute(getRequest);
  6.         final int statusCode = response.getStatusLine().getStatusCode();
  7.         if (statusCode != HttpStatus.SC_OK) {  
  8.             Log.e("cwjDebug", "Error " + statusCode + " while retrieving bitmap from " + url);  
  9.             return null;
  10.         }
  11.          
  12.         final HttpEntity entity = response.getEntity();
  13.         if (entity != null) {
  14.             InputStream inputStream = null;
  15.             try {
  16.                 inputStream = entity.getContent();  
  17.                 final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
  18.                 return bitmap;
  19.             } finally {
  20.                 if (inputStream != null) {
  21.                     inputStream.close();   
  22.                 }
  23.                 entity.consumeContent();
  24.             }
  25.         }
  26.     } catch (Exception e) {
  27.           getRequest.abort();
  28.         Log.e("Debug", "Error while retrieving bitmap from " + url, e.toString());
  29.     } finally {
  30.         if (client != null) {
  31.             client.close();
  32.         }
  33.     }
  34.     return null;
  35. }
复制代码

这里提醒大家,BitmapFactory类的decodeStream方法在网络超时或较慢的时候无法获取完整的数据,这里我们通过继承FilterInputStream类的skip方法来强制实现flush流中的数据,主要原理就是检查是否到文件末端,告诉http类是否继续。

  1. static class FlushedInputStream extends FilterInputStream {
  2.     public FlushedInputStream(InputStream inputStream) {
  3.         super(inputStream);
  4.     }
  5.     @Override
  6.     public long skip(long n) throws IOException {
  7.         long totalBytesSkipped = 0L;
  8.         while (totalBytesSkipped < n) {
  9.             long bytesSkipped = in.skip(n - totalBytesSkipped);
  10.             if (bytesSkipped == 0L) {
  11.                   int byte = read();
  12.                   if (byte < 0) {
  13.                       break;  // we reached EOF
  14.                   } else {
  15.                       bytesSkipped = 1; // we read one byte
  16.                   }
  17.            }
  18.             totalBytesSkipped += bytesSkipped;
  19.         }
  20.         return totalBytesSkipped;
  21.     }
  22. }
复制代码

方法二、通过AsyncTask的异步任务

  从Android 1.5固件开发平台开始Google提供了一个AsyncTask类来帮助开发者处理异步下载的实现,相对于Thread线程而言他可以运行在UI线程中,其内部的实现是从Java 5开始的并发包concurrent中派生而来的,总体实现比较可靠就是资源占用略大了些。不过使用起来比简单。这里下载图片类ImageDownloader类的download方法可以很好的处理实现UI显示等操作,参数一url为远程server上文件的url,第二个参数为imageview对象,可以直接让imageview显示出下载的远程图片。

  1. public class ImageDownloader {
  2.     public void download(String url, ImageView imageView) {
  3.             BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
  4.             task.execute(url);
  5.         }
  6.     }
  7. }
复制代码

有关具体的AsyncTask类实现,考虑到图片可能较大,为了给JVM充分的空间存储,这里推荐大家使用弱引用来保存ImageView对象。

  1. class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
  2.     private String url;
  3.     private final WeakReference<ImageView> imageViewReference;  //使用WeakReference解决内存问题
  4.     public BitmapDownloaderTask(ImageView imageView) {
  5.         imageViewReference = new WeakReference<ImageView>(imageView);
  6.     }
  7.   @Override
  8.     protected Bitmap doInBackground(String... params) {   //实际的下载线程,内部其实是concurrent线程,所以不会阻塞
  9.    
  10.          return downloadBitmap(params[0]);   
  11.   }
  12.     @Override
  13.      protected void onPostExecute(Bitmap bitmap) {   //下载完后执行的
  14.         if (isCancelled()) {
  15.             bitmap = null;
  16.         }
  17.         if (imageViewReference != null) {
  18.             ImageView imageView = imageViewReference.get();
  19.             if (imageView != null) {
  20.                 imageView.setImageBitmap(bitmap);  //下载完设置imageview为刚才下载的bitmap对象
  21.             }
  22.         }
  23.     }
  24. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值