Improved ImageDownloader code.

Integrate comments from the blog article.

Change-Id: If51d27a4ef3eb0df0bc660ea37a85cc39fd18064
This commit is contained in:
Gilles Debunne
2010-07-08 16:24:07 -07:00
parent f290e5c6b1
commit fa640af482

View File

@@ -21,6 +21,7 @@ import org.apache.http.client.methods.HttpGet;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.http.AndroidHttpClient; import android.net.http.AndroidHttpClient;
@@ -250,17 +251,23 @@ public class ImageDownloader {
try { try {
HttpResponse response = client.execute(getRequest); HttpResponse response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final int statusCode = response.getStatusLine().getStatusCode();
final HttpEntity entity = response.getEntity(); if (statusCode != HttpStatus.SC_OK) {
if (entity != null) { Log.w("ImageDownloader", "Error " + statusCode +
final InputStream inputStream = entity.getContent(); " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = entity.getContent();
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
final OutputStream out = outputStream = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); copy(inputStream, outputStream);
copy(inputStream, out); outputStream.flush();
out.flush();
out.close();
inputStream.close();
final byte[] data = dataStream.toByteArray(); final byte[] data = dataStream.toByteArray();
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
@@ -269,6 +276,15 @@ public class ImageDownloader {
//final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); //final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap; return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
entity.consumeContent();
} }
} }
} catch (IOException e) { } catch (IOException e) {
@@ -334,7 +350,7 @@ public class ImageDownloader {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference; private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) { public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
super(0); super(Color.BLACK);
bitmapDownloaderTaskReference = bitmapDownloaderTaskReference =
new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask); new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
} }