summaryrefslogtreecommitdiffstats
path: root/samples/XmlAdapters
diff options
context:
space:
mode:
authorGilles Debunne <debunne@google.com>2010-07-08 16:24:07 -0700
committerGilles Debunne <debunne@google.com>2010-07-08 16:25:05 -0700
commitfa640af48228e80ece595351999ef6c1a2767d53 (patch)
treea4305633db4ce9ad5e52aa3c95b1d70e56bb9bdd /samples/XmlAdapters
parentf290e5c6b1eaac1eee7a572ef56ea78f736c685a (diff)
downloadandroid_development-fa640af48228e80ece595351999ef6c1a2767d53.tar.gz
android_development-fa640af48228e80ece595351999ef6c1a2767d53.tar.bz2
android_development-fa640af48228e80ece595351999ef6c1a2767d53.zip
Improved ImageDownloader code.
Integrate comments from the blog article. Change-Id: If51d27a4ef3eb0df0bc660ea37a85cc39fd18064
Diffstat (limited to 'samples/XmlAdapters')
-rw-r--r--samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.java38
1 files changed, 27 insertions, 11 deletions
diff --git a/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.java b/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.java
index 08e144c7f..c84f9d528 100644
--- a/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.java
+++ b/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.java
@@ -21,6 +21,7 @@ import org.apache.http.client.methods.HttpGet;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.http.AndroidHttpClient;
@@ -250,17 +251,23 @@ public class ImageDownloader {
try {
HttpResponse response = client.execute(getRequest);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- final HttpEntity entity = response.getEntity();
- if (entity != null) {
- final InputStream inputStream = entity.getContent();
+ 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;
+ OutputStream outputStream = null;
+ try {
+ inputStream = entity.getContent();
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
- final OutputStream out =
- new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
- copy(inputStream, out);
- out.flush();
- out.close();
- inputStream.close();
+ outputStream = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
+ copy(inputStream, outputStream);
+ outputStream.flush();
final byte[] data = dataStream.toByteArray();
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
@@ -269,6 +276,15 @@ public class ImageDownloader {
//final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
+
+ } finally {
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ if (outputStream != null) {
+ outputStream.close();
+ }
+ entity.consumeContent();
}
}
} catch (IOException e) {
@@ -334,7 +350,7 @@ public class ImageDownloader {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
- super(0);
+ super(Color.BLACK);
bitmapDownloaderTaskReference =
new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
}