android:利用異步同時下載多張圖片(07) android:利用異步同時下載多張圖片,doInBackground()方法下載後返回的時候返回一個list //佈局文件就是四個ImageView //第一種返回的是一個數組的集合,再轉換成位圖 //第二種返回的直接是位圖集合 public class MainActivity extends Activity { private ImageView image_1; private ImageView image_2; private ImageView image_3; private ImageView image_4; private String urlString1 = "http://www.touxiang.cn/uploads/20130608/08-023618_517.jpg"; private String urlString2 = "http://www.touxiang.cn/uploads/20140216/16-075941_383-lp.jpg"; private String urlString3 = "http://www.touxiang.cn/uploads/20140216/16-082440_368-lp.jpg"; private String urlString4 = "http://www.touxiang.cn/uploads/20140216/16-063601_933-lp.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.image_1 = (ImageView) this.findViewById(R.id.image_1); this.image_2 = (ImageView) this.findViewById(R.id.image_2); this.image_3 = (ImageView) this.findViewById(R.id.image_3); this.image_4 = (ImageView) this.findViewById(R.id.image_4); /* 也能夠定義爲數組 String[] url = { "http://litchiapi.jstv.com/Attachs/Top/11949/e650e0201de541d2ba91dca202b0fcfe.jpg", "http://192.168.10.85:8080/xml/login.jpeg", "http://192.168.10.85:8080/xml/ss.jpg", "http://g.hiphotos.baidu.com/image/h%3D200/sign=bff104116009c93d18f209f7af3cf8bb/024f78f0f736afc30fd7717ab419ebc4b64512d1.jpg" }; //"http://192.168.10.85:8080/xml/ss.jpg"本地服務器的圖片 */ new myTask().execute(urlString1,urlString2,urlString3,urlString4); } class myTask extends AsyncTask<String, Integer, List<byte[]>> { @Override protected List<byte[]> doInBackground(String... params) { List<byte[]> list = new ArrayList<byte[]>(); HttpClient client = new DefaultHttpClient(); for (int i = 0; i < params.length; i++) { HttpGet get = new HttpGet(params[i]); try { HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { list.add(EntityUtils.toByteArray(response.getEntity())); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return list; } @Override protected void onPostExecute(List<byte[]> result) { if (result != null) { Bitmap bitmap_1 = BitmapFactory.decodeByteArray(result.get(0), 0, result.get(0).length); image_1.setImageBitmap(bitmap_1); Bitmap bitmap_2 = BitmapFactory.decodeByteArray(result.get(1), 0, result.get(1).length); image_2.setImageBitmap(bitmap_2); Bitmap bitmap_3 = BitmapFactory.decodeByteArray(result.get(2), 0, result.get(2).length); image_3.setImageBitmap(bitmap_3); Bitmap bitmap_4 = BitmapFactory.decodeByteArray(result.get(3), 0, result.get(3).length); image_4.setImageBitmap(bitmap_4); } else { Toast.makeText(MainActivity.this, "網絡錯誤!!", Toast.LENGTH_SHORT) .show(); } } } } //第二種方法返回的直接是位圖集合:以下: protected List<Bitmap> doInBackground(String... params) { List<Bitmap> list = new ArrayList<Bitmap>(); for (int i = 0; i < params.length; i++) { try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[i]); HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); byte[] data = EntityUtils.toByteArray(entity); Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); list.add(bm); } } catch (Exception e) { e.printStackTrace(); } } return list; } @Override protected void onPostExecute(List<Bitmap> result) { super.onPostExecute(result); if (result != null) { imageView_1.setImageBitmap(result.get(0)); imageView_2.setImageBitmap(result.get(1)); imageView_3.setImageBitmap(result.get(2)); imageView_4.setImageBitmap(result.get(3)); } else { Toast.makeText(context, "網絡異常,加載數據失敗!", Toast.LENGTH_LONG) .show(); } pDialog.dismiss(); } }