我想用Asynctask下载许多位图文件。 (此位图位于网络上)
如果那么我该怎么办呢?
private ArrayList<String> bitmapUrls;
private ArrayList<Bitmap> bitarray;
..
bitmapUrls.add("Here's my bitmap url");
x 10
..
public class DownloadImages extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
for(String biturl:bitmapUrls){
bitmap = downloadBitmap(biturl);
return bitmap;
}
}
@Override
protected void onPostExecute(Bitmap result) {
bitarray.add(result);
}
public Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient
.newInstance("Android");
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "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();
} finally {
if (client != null) {
client.close();
}
}
return null;
}
}
如您所见,我真的想在我的代码中获取bitarray结果数组。 请给我一个改进这个东西代码的建议。