Android Open Source - SandB-Android Universal Loader Utility






From Project

Back to project page SandB-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project SandB-Android 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 edu.grinnell.sandb.img;
/* ww w . j av  a 2s .  c  om*/
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import edu.grinnell.sandb.R;
import edu.grinnell.sandb.data.Article;

public class UniversalLoaderUtility {

  protected ImageLoader imageLoader = ImageLoader.getInstance();
  protected ProgressBar spinner = null;

  public UniversalLoaderUtility() {
  }

  public ImageLoader getImageLoader() {
    return imageLoader;
  }

  protected SimpleImageLoadingListener listener = new SimpleImageLoadingListener() {

    @Override
    public void onLoadingFailed(String imageUri, View view,
        FailReason failReason) {
      String message = null;
      switch (failReason.getType()) {
      case IO_ERROR:
        message = "Input/Output error";
        break;
      case DECODING_ERROR:
        message = "Image can't be decoded";
        break;
      case NETWORK_DENIED:
        message = "Downloads are denied";
        break;
      case OUT_OF_MEMORY:
        message = "Out Of Memory error";
        break;
      case UNKNOWN:
        message = "Unknown error";
        break;
      }
      Animation a = ((ImageView) view).getAnimation();
      if (a != null)
        view.clearAnimation();
        //a.cancel();
      view.setVisibility(View.GONE);
    }
  };

  // load image based on URL
  public void loadImage(String imgUrl, ImageView imgView, Context context) {

    DisplayImageOptions options;

    options = new DisplayImageOptions.Builder()
        .imageScaleType(ImageScaleType.EXACTLY)
        .resetViewBeforeLoading().cacheOnDisc()
        .imageScaleType(ImageScaleType.EXACTLY)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .displayer(new FadeInBitmapDisplayer(300)).build();

    spinner = new ProgressBar(context, null,
        android.R.attr.progressBarStyleSmall);

    imageLoader.displayImage(imgUrl, imgView, options, listener);
  }

  // load first image from an article, in low res
  public void loadArticleImage(Article a, ImageView imgView, Context context) {
    ImageTable imgTable = new ImageTable(imgView.getContext());
    imgTable.open();

    int id = a.getId();
    String[] URLS = imgTable.findUrlsByArticleId(id);
    imgTable.close();

    // try {
    if (URLS != null) {
      // throw exception if no image
      String imgUrl = URLS[0];

      DisplayImageOptions options;

      options = new DisplayImageOptions.Builder()
          .imageScaleType(ImageScaleType.EXACTLY)
          // change these images to error messages
          .showImageOnFail(R.drawable.sandblogo)
          .resetViewBeforeLoading().cacheOnDisc()
          .imageScaleType(ImageScaleType.EXACTLY)
          .bitmapConfig(Bitmap.Config.RGB_565)
          .displayer(new FadeInBitmapDisplayer(300)).build();

      imageLoader.displayImage(imgUrl, imgView, options, listener);
      imgView.setVisibility(View.VISIBLE);

    }
    // catch (NullPointerException e) {
    else {
      imgTable.close();
      // imageLoader.displayImage(null, imgView, null, listener);
      imgView.setVisibility(View.GONE);
    }
  }

  // load first image from an article, in low res
  public void loadHiResArticleImage(Article a, ImageView imgView,
      Context context) {

    ImageTable imgTable = new ImageTable(imgView.getContext());
    imgTable.open();

    int id = a.getId();
    String[] URLS = imgTable.findUrlsByArticleId(id);

    try {
      // throw exception if no image
      String imgUrl = URLS[0];
      String hiResImgUrl = getHiResImage(imgUrl);

      DisplayImageOptions options;

      options = new DisplayImageOptions.Builder()
          .showStubImage(R.drawable.loading).resetViewBeforeLoading()
          .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
          .bitmapConfig(Bitmap.Config.RGB_565)
          .displayer(new FadeInBitmapDisplayer(300)).build();

      imgView.startAnimation(AnimationUtils.loadAnimation(context,
          R.anim.loading));

      spinner = new ProgressBar(context, null,
          android.R.attr.progressBarStyleSmall);

      imageLoader.displayImage(hiResImgUrl, imgView, options, listener);
      imgView.setVisibility(View.VISIBLE);
      imgTable.close();

    } catch (NullPointerException e) {
      imgTable.close();
      // imageLoader.displayImage(null, imgView, null, listener);
      imgView.setVisibility(View.GONE);
    }
  }

  // load first image from an article, in low res
  public void loadHiResArticleImage(String url, ImageView imgView,
      Context context) {

    ImageTable imgTable = new ImageTable(imgView.getContext());
    imgTable.open();

    try {
      // throw exception if no image
      String hiResImgUrl = getHiResImage(url);

      DisplayImageOptions options;

      options = new DisplayImageOptions.Builder()
          .showStubImage(R.drawable.loading).resetViewBeforeLoading()
          .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
          .bitmapConfig(Bitmap.Config.RGB_565)
          .displayer(new FadeInBitmapDisplayer(300)).build();

      imgView.startAnimation(AnimationUtils.loadAnimation(context,
          R.anim.loading));

      spinner = new ProgressBar(context, null,
          android.R.attr.progressBarStyleSmall);

      imageLoader.displayImage(hiResImgUrl, imgView, options, listener);
      imgView.setVisibility(View.VISIBLE);
      imgTable.close();

    } catch (NullPointerException e) {
      imgTable.close();
      // imageLoader.displayImage(null, imgView, null, listener);
      imgView.setVisibility(View.GONE);
    }
  }

  // remove the ends of each image URL to download full sized images
  public String getHiResImage(String lowResImg) {
    if (lowResImg == null)
      return null;
    
    // do nothing if there is no resolution tag
    if (!lowResImg.contains("-")  || !lowResImg.contains("x"))
      return lowResImg;

    int readTo = lowResImg.lastIndexOf("-");

    if (readTo != -1) {
      String hiResImg = lowResImg.substring(0, readTo);
      hiResImg = hiResImg.concat(".jpg");
      return hiResImg;
    } else
      return lowResImg;
  }
}




Java Source Code List

edu.grinnell.sandb.ArticleDetailActivity.java
edu.grinnell.sandb.ArticleDetailFragment.java
edu.grinnell.sandb.ArticleListAdapter.java
edu.grinnell.sandb.ArticleListFragment.java
edu.grinnell.sandb.CommentListAdapter.java
edu.grinnell.sandb.CommentListFragment.java
edu.grinnell.sandb.ImagePagerActivity.java
edu.grinnell.sandb.MainActivity.java
edu.grinnell.sandb.MainPrefs.java
edu.grinnell.sandb.ScarletAndBlackApplication.java
edu.grinnell.sandb.Utility.java
edu.grinnell.sandb.comments.CommentStorageHelper.java
edu.grinnell.sandb.comments.CommentTable.java
edu.grinnell.sandb.comments.Comment.java
edu.grinnell.sandb.data.ArticleStorageHelper.java
edu.grinnell.sandb.data.ArticleTable.java
edu.grinnell.sandb.data.Article.java
edu.grinnell.sandb.img.BodyImageGetter.java
edu.grinnell.sandb.img.ImageStorageHelper.java
edu.grinnell.sandb.img.ImageTable.java
edu.grinnell.sandb.img.Image.java
edu.grinnell.sandb.img.UniversalLoaderUtility.java
edu.grinnell.sandb.xmlpull.CommentParseTask.java
edu.grinnell.sandb.xmlpull.XmlCheckAgeTask.java
edu.grinnell.sandb.xmlpull.XmlFetchTask.java
edu.grinnell.sandb.xmlpull.XmlParseTask.java
edu.grinnell.sandb.xmlpull.XmlPullReceiver.java
edu.grinnell.sandb.xmlpull.XmlPullService.java