Android Open Source - MobileCodeGenerator Image Downloading Task






From Project

Back to project page MobileCodeGenerator.

License

The source code is released under:

GNU General Public License

If you think the Android project MobileCodeGenerator 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 it.polimi.valtellina;
/*from   w w  w .java2 s.  c  o  m*/
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class ImageDownloadingTask extends AsyncTask<ImageView, Void, Bitmap> {
  private ImageView imageView;
  private String imageUri;

  /**
   * This task attempts to download an image from the Network:
   * in case of success, it sets the proper ImageView,
   * in case of failure, it does nothing (ImageView remains without image).
   */
  public ImageDownloadingTask() {
  };

  /***
   * Attempts to download the image from the Network.
   * @params params[0] = ImageView where to put the image to be downloaded. The url must be attached in a tag on the ImageView.
   */
  @Override
  protected Bitmap doInBackground(ImageView... params) {
    imageView = (ImageView) params[0];
    Bitmap bitmap = null;
    if (imageView != null) {
      imageUri = imageView.getTag().toString();
      try {
        bitmap = downloadImage(imageUri);
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) { //failure in downloading the image
        e.printStackTrace();
      }
    }
    return bitmap;
  }

  /**
   * Attempts to download the image at the url passed as parameter: it returns the bitmap in case of success, null otherwise.
   * @param imageUrl
   */
  protected Bitmap downloadImage(String imageUrl)
      throws MalformedURLException, IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(imageUrl)
        .openConnection();
    connection.connect();
    Bitmap downloadedBitmap = BitmapFactory.decodeStream(connection
        .getInputStream()); //Convert to bitmap
    connection.disconnect();
    return downloadedBitmap;
  }

  /***
   * If the image has been downloaded properly it sets the ImageView.
   */
  @Override
  protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    if (bitmap != null) {
      String imageUri = imageView.getTag().toString();
      if (this.imageUri.equals(imageUri)) { //needed for synchronization reasons
        this.imageView.setImageBitmap(bitmap);
      }
    }
  }
}




Java Source Code List

android_extensions.AndroidConstants.java
android_extensions.AndroidJavaUtil.java
app_extensions.AppJavaUtil.java
ios_extensions.IOSConstants.java
ios_extensions.IOSJavaUtil.java
it.polimi.multimedia.AudioListActivity.java
it.polimi.multimedia.AudioListActivity.java
it.polimi.multimedia.AudioPlayerActivity.java
it.polimi.multimedia.AudioPlayerActivity.java
it.polimi.multimedia.AudioRecorderActivity.java
it.polimi.multimedia.AudioRecorderActivity.java
it.polimi.multimedia.GalleryActivity.java
it.polimi.multimedia.GalleryActivity.java
it.polimi.multimedia.MenuActivity.java
it.polimi.multimedia.MenuActivity.java
it.polimi.multimedia.PhotoGridActivity.java
it.polimi.multimedia.PhotoGridActivity.java
it.polimi.multimedia.PhotoViewerActivity.java
it.polimi.multimedia.PhotoViewerActivity.java
it.polimi.multimedia.PhotocameraActivity.java
it.polimi.multimedia.PhotocameraActivity.java
it.polimi.multimedia.Utils.java
it.polimi.multimedia.Utils.java
it.polimi.multimedia.VideoListActivity.java
it.polimi.multimedia.VideoListActivity.java
it.polimi.multimedia.VideoViewerActivity.java
it.polimi.multimedia.VideoViewerActivity.java
it.polimi.multimedia.VideocameraActivity.java
it.polimi.multimedia.VideocameraActivity.java
it.polimi.promemoria.HomeActivity.java
it.polimi.promemoria.NoteDetailActivity.java
it.polimi.promemoria.NotesListActivity.java
it.polimi.promemoria.Utils.java
it.polimi.valtellina.DescriptionActivity.java
it.polimi.valtellina.DescriptionActivity.java
it.polimi.valtellina.ImageDownloadingTask.java
it.polimi.valtellina.ImageDownloadingTask.java
it.polimi.valtellina.MenuActivity.java
it.polimi.valtellina.MenuActivity.java
it.polimi.valtellina.TurismLocationDetailActivity.java
it.polimi.valtellina.TurismLocationDetailActivity.java
it.polimi.valtellina.TurismLocationsListActivity.java
it.polimi.valtellina.TurismLocationsListActivity.java
it.polimi.valtellina.Utils.java
it.polimi.valtellina.Utils.java
it.polimi.valtellina.VideoActivity.java
it.polimi.valtellina.VideoActivity.java
model_checks.JavaChecks.java