Androidの下のマルチスレッドはリモート画像をダウンロードします。


クリックしてリンクを開く
          Android                   ,                        。

     、    Android     Http         ,  AndroidHttpClient SDK 2.2      ,API Level 8,       ,          ,  SDK        Apache Http ,  HttpURLConnection URLConnection   。

    static Bitmap downloadBitmapByCwj(String url) {
        final AndroidHttpClient client = AndroidHttpClient.newInstance("linux    ");
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {  
                Log.e("cwjDebug", "Error " + statusCode + " while retrieving bitmap from " + url);  
                return null;
            }
             
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();  
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();   
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
              getRequest.abort();
            Log.e("Debug", "Error while retrieving bitmap from " + url, e.toString());
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }


      ,BitmapFactory  decodeStream                      ,        FilterInputStream  skip       flush     ,               ,  http     。

    static class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }

        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                      int byte = read();
                      if (byte < 0) {
                          break;  // we reached EOF
                      } else {
                          bytesSkipped = 1; // we read one byte
                      }
               }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }


   、  AsyncTask     

   Android 1.5        Google     AsyncTask                ,   Thread          UI   ,        Java 5      concurrent      ,                  。         。       ImageDownloader  download           UI     ,   url   server    url,      imageview  ,     imageview          。

    public class ImageDownloader {

        public void download(String url, ImageView imageView) {
                BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
                task.execute(url);
            }
        }

    }


     AsyncTask   ,         ,   JVM       ,              ImageView  。

    class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private String url;
        private final WeakReference<ImageView> imageViewReference;  //  WeakReference      

        public BitmapDownloaderTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }


      @Override
        protected Bitmap doInBackground(String... params) {   //       ,     concurrent  ,      
       
             return downloadBitmap(params[0]);   

      }

        @Override
         protected void onPostExecute(Bitmap bitmap) {   //       
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);  //     imageview      bitmap  
                }
            }
        }
    }