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();
}
}
转载于:https://my.oschina.net/u/2541146/blog/604305