Android Open Source - MyFlickrSearch Download Flickr Images Task






From Project

Back to project page MyFlickrSearch.

License

The source code is released under:

MIT License

If you think the Android project MyFlickrSearch listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.devng.flickrsearch.task;
//from  ww w .j a v  a2s.  c o m
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;

import com.devng.flickrsearch.common.Enums;
import com.devng.flickrsearch.common.Helpers;
import com.devng.flickrsearch.model.FlickrImgRef;

public class DownloadFlickrImagesTask extends AsyncTask<FlickrImgRef, Pair<Bitmap, FlickrImgRef>, Void> {

  private static final String LOG_TAG = DownloadFlickrImagesTask.class.getSimpleName();

  private final DownloadFlickrImagesHandler handler;

  private final Enums.ImageSize imageSize;

  public DownloadFlickrImagesTask(DownloadFlickrImagesHandler handler, Enums.ImageSize imageSize) {
    this.handler = handler;
    this.imageSize = imageSize;
  }

  @SuppressWarnings("unchecked")
  @Override
  protected Void doInBackground(FlickrImgRef... imgRefs) {
    for (int i = 0; i < imgRefs.length; i++) {
      String imgUrl = Helpers.constructFlickrImgUrl(imgRefs[i], this.imageSize);
      Bitmap bitmap = this.downloadBitmap(imgUrl);
      publishProgress(new Pair<Bitmap, FlickrImgRef>(bitmap, imgRefs[i]));
    }
    return null;
  }

  private Bitmap downloadBitmap(String url) {
    Log.d(LOG_TAG, "Downloading image from url: " + url);
    byte[] response = Helpers.doGet(url);
    if (response == null || response.length == 0) {
      return null;
    }
    Options opts = new Options();
    // opts.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(response, 0, response.length, opts);
    return bitmap;
  }

  @Override
  protected void onProgressUpdate(Pair<Bitmap, FlickrImgRef>... pairs) {
    super.onProgressUpdate(pairs);
    if (pairs != null) {
      for (Pair<Bitmap, FlickrImgRef> pair : pairs) {
        handler.onDownloadedFlickrImage(pair);
      }
    }
  }

  @Override
  protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    handler.onDownloadFinish();
  }
}




Java Source Code List

com.devng.flickrsearch.AbstractBackButtonActivity.java
com.devng.flickrsearch.ImageViewerActivity.java
com.devng.flickrsearch.MainActivity.java
com.devng.flickrsearch.MainModule.java
com.devng.flickrsearch.SearchResultsActivity.java
com.devng.flickrsearch.SearchResultsListAdapter.java
com.devng.flickrsearch.common.Config.java
com.devng.flickrsearch.common.Enums.java
com.devng.flickrsearch.common.Helpers.java
com.devng.flickrsearch.model.FlickrImgContainer.java
com.devng.flickrsearch.model.FlickrImgRef.java
com.devng.flickrsearch.model.FlickrImgRoot.java
com.devng.flickrsearch.task.DownloadFlickrImagesHandler.java
com.devng.flickrsearch.task.DownloadFlickrImagesTask.java
com.devng.flickrsearch.task.SearchFlickrResultHadler.java
com.devng.flickrsearch.task.SearchFlickrTask.java