Created
October 19, 2012 15:10
-
-
Save daniel-c05/3918743 to your computer and use it in GitHub Desktop.
Helper class to download an image from a specified URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BitmapHelper { | |
class DownloadImageFromURL extends AsyncTask<String, String, Bitmap> { | |
private final WeakReference<ImageView> imageViewReference; | |
String mUrl; | |
private int mWidth = 0; | |
private int mHeigth = 0; | |
private boolean isThumbnail = false; | |
public DownloadImageFromURL (String url, ImageView imageview) { | |
mUrl = url; | |
imageViewReference = new WeakReference<ImageView>(imageview); | |
} | |
public DownloadImageFromURL (String url, ImageView imageview, int rWidth, int rHeight) { | |
mUrl = url; | |
imageViewReference = new WeakReference<ImageView>(imageview); | |
isThumbnail = true; | |
mWidth = rWidth; | |
mHeigth = rHeight; | |
} | |
@Override | |
protected Bitmap doInBackground(String... params) { | |
if (isThumbnail) { | |
return DownloadImageFromUrl(mUrl, mWidth, mHeigth); | |
} | |
return DownloadImageFromUrl(mUrl); | |
} | |
@Override | |
protected void onPostExecute(Bitmap bitmap) { | |
if(isCancelled()) { | |
bitmap = null; | |
} | |
if (imageViewReference != null) { | |
ImageView imageView = imageViewReference.get(); | |
DownloadImageFromURL bitmapDownloaderTask = getDownloaderTask(imageView); | |
if (this == bitmapDownloaderTask) { | |
imageView.setImageBitmap(bitmap); | |
} | |
} | |
} | |
} | |
private static DownloadImageFromURL getDownloaderTask(ImageView imageView) { | |
if (imageView != null) { | |
Drawable drawable = imageView.getDrawable(); | |
if (drawable instanceof DownloadedDrawable) { | |
DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable; | |
return downloadedDrawable.getTask(); | |
} | |
} | |
return null; | |
} | |
static class DownloadedDrawable extends ColorDrawable { | |
private final WeakReference<DownloadImageFromURL> taskReference; | |
public DownloadedDrawable(DownloadImageFromURL task) { | |
super(Color.BLACK); | |
taskReference = | |
new WeakReference<DownloadImageFromURL>(task); | |
} | |
public DownloadImageFromURL getTask() { | |
return taskReference.get(); | |
} | |
} | |
private static boolean cancelPotentialDownload(String url, ImageView imageView) { | |
DownloadImageFromURL downloaderTask = getDownloaderTask(imageView); | |
if (downloaderTask != null) { | |
String bitmapUrl = downloaderTask.mUrl; | |
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) { | |
downloaderTask.cancel(true); | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} | |
public void download(String url, ImageView imageView, boolean thumbnail) { | |
if (cancelPotentialDownload(url, imageView)) { | |
DownloadImageFromURL task; | |
if (thumbnail) { | |
task = new DownloadImageFromURL(url, imageView, 48, 48); | |
} | |
else { | |
task = new DownloadImageFromURL(url, imageView); | |
} | |
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task); | |
imageView.setImageDrawable(downloadedDrawable); | |
task.execute(); | |
} | |
} | |
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) { | |
if (width > height) { | |
inSampleSize = Math.round((float)height / (float)reqHeight); | |
} else { | |
inSampleSize = Math.round((float)width / (float)reqWidth); | |
} | |
} | |
return inSampleSize; | |
} | |
public Bitmap decodeSampledBitmapFromResource(byte [] data, int reqWidth, int reqHeight) { | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeByteArray(data, 0, data.length, options); | |
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); | |
options.inJustDecodeBounds = false; | |
return BitmapFactory.decodeByteArray(data, 0, data.length, options); | |
} | |
public Bitmap DownloadImageFromUrl (String url, int width, int height) { | |
Bitmap bitmap = null; | |
HttpUriRequest request; | |
HttpClient httpClient; | |
HttpResponse response; | |
StatusLine statusLine = null; | |
int statusCode = 0; | |
try { | |
request = new HttpGet(url.toString()); | |
httpClient = new DefaultHttpClient(); | |
response = httpClient.execute(request); | |
statusLine = response.getStatusLine(); | |
statusCode = statusLine.getStatusCode(); | |
if (statusCode == 200) { | |
HttpEntity entity = response.getEntity(); | |
byte[] bytes = EntityUtils.toByteArray(entity); | |
bitmap = decodeSampledBitmapFromResource(bytes, width, height); | |
} | |
} | |
catch (Exception e) { | |
Log.v(Constants.TAG, ("Download failed, HTTP response code " + statusCode + " - " + statusLine.getReasonPhrase())); | |
} | |
return bitmap; | |
} | |
public Bitmap DownloadImageFromUrl (String url) { | |
Bitmap bitmap = null; | |
HttpUriRequest request; | |
HttpClient httpClient; | |
HttpResponse response; | |
StatusLine statusLine = null; | |
int statusCode = 0; | |
try { | |
request = new HttpGet(url.toString()); | |
httpClient = new DefaultHttpClient(); | |
response = httpClient.execute(request); | |
statusLine = response.getStatusLine(); | |
statusCode = statusLine.getStatusCode(); | |
if (statusCode == 200) { | |
HttpEntity entity = response.getEntity(); | |
byte[] bytes = EntityUtils.toByteArray(entity); | |
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); | |
} | |
} | |
catch (Exception e) { | |
Log.v(Constants.TAG, ("Download failed, HTTP response code " + statusCode + " - " + statusLine.getReasonPhrase())); | |
} | |
return bitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment