Back to project page WineDB.
The source code is released under:
MIT License
If you think the Android project WineDB listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.selesse.android.winedb.async; /* w ww .j a va 2s . c o m*/ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class AsyncImageLoader extends AsyncTask<String, Void, Bitmap> { private static final String TAG = AsyncImageLoader.class.getSimpleName(); private final ImageView imageView; public AsyncImageLoader(ImageView imageView) { this.imageView = imageView; } @Override protected void onPreExecute() { super.onPreExecute(); imageView.setImageBitmap(null); } @Override protected Bitmap doInBackground(String... strings) { String url = strings[0]; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inSampleSize = 1; return loadImage(url, bmOptions); } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (bitmap != null) { imageView.setImageBitmap(bitmap); } } private Bitmap loadImage(String imageUrl, BitmapFactory.Options options) { Bitmap bitmap = null; try { InputStream in = openHttpConnection(imageUrl); if (in == null) { return null; } bitmap = BitmapFactory.decodeStream(in, null, options); in.close(); } catch (IOException e) { Log.e(TAG, "Error loading image " + imageUrl, e); } return bitmap; } private InputStream openHttpConnection(String streamUrl) { InputStream inputStream = null; try { URL url = new URL(streamUrl); URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("GET"); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = httpConn.getInputStream(); } } catch (IOException e) { Log.e(TAG, "Error fetching image " + streamUrl, e); } return inputStream; } }