Android Open Source - Resonos-Android-Framework Image Loader






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework 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.resonos.apps.library.media;
// w  w  w . j a v a  2 s  .com
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.resonos.apps.library.App;
import com.resonos.apps.library.file.FileCache;

/**
 * Class that asynchronously loads images into ImageViews or as the background
 * of other views. Heavily based on LazyList at
 * https://github.com/thest1/LazyList/ With the addition of an ImageGenerator
 * alternative to downloading an image.
 */
public class ImageLoader {

  BitmapMemoryCache memoryCache = new BitmapMemoryCache();
  FileCache fileCache;
  private Map<View, String> imageViews = Collections
      .synchronizedMap(new WeakHashMap<View, String>());
  ExecutorService executorService;
  Drawable mStub;
  Resources res;

  public ImageLoader(App app, Drawable stub) {
    fileCache = new FileCache(app);
    executorService = app.mExecutorService != null
        ? app.mExecutorService : Executors.newFixedThreadPool(5);
    mStub = stub;
    res = app.getContext().getResources();
  }

  public void DisplayImageURL(String url, View imageView, int width) {
    PhotoToLoad p = new PhotoToLoad(url, imageView, width);
    DisplayImage(p, imageView);
  }

  public void DisplayImageGen(ImageGenerator iGen, String param, View image,
      int width) {
    PhotoToLoad p = new PhotoToLoad(iGen, param, image, width);
    DisplayImage(p, image);
  }

  private void DisplayImage(PhotoToLoad p, View imageView) {
    Bitmap bitmap = memoryCache.get(p.getTag());
    if (bitmap != null) {
      // M.log("ImageLoader", "DisplayImage " + p.getTag());
      imageViews.put(imageView, p.getTag());
      if (imageView instanceof ImageView)
        ((ImageView) imageView).setImageBitmap(bitmap);
      else
        imageView
            .setBackgroundDrawable(new BitmapDrawable(res, bitmap));
    } else {
      String oldTag = imageViews.get(imageView);
      if (oldTag != null)
        if (oldTag.equals(p.getTag()))
          return;
      // M.log("ImageLoader", "QueueImage " + p.getTag());
      imageViews.put(imageView, p.getTag());
      queuePhoto(p, imageView);
      if (imageView instanceof ImageView)
        ((ImageView) imageView).setImageDrawable(mStub);
      else
        imageView.setBackgroundDrawable(mStub);
    }
  }

  private void queuePhoto(PhotoToLoad p, View imageView) {
    executorService.submit(new PhotosLoader(p));
  }

  public interface ImageGenerator {
    public Bitmap generateImage(String param);

    public String getTag();
  }

  private Bitmap downloadBitmap(File f, String url, int width) {
    // from web
    try {
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl
          .openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      OutputStream os = new FileOutputStream(f);
      CopyStream(is, os);
      os.close();
      bitmap = decodeFile(f, width);
      return bitmap;
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }

  // decodes image and scales it to reduce memory consumption
  private Bitmap decodeFile(File f, int width) {
    try {
      // decode image size
      BitmapFactory.Options o = new BitmapFactory.Options();
      o.inJustDecodeBounds = true;
      BitmapFactory.decodeStream(new FileInputStream(f), null, o);

      // Find the correct scale value. It should be the power of 2.
      final int REQUIRED_SIZE = width == 0 ? o.outWidth : width;
      int width_tmp = o.outWidth, height_tmp = o.outHeight;
      int scale = 1;
      while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE
            || height_tmp / 2 < REQUIRED_SIZE)
          break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
      }

      // decode with inSampleSize
      BitmapFactory.Options o2 = new BitmapFactory.Options();
      o2.inSampleSize = scale;
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
  }

  // Task for the queue
  private class PhotoToLoad {
    private String url;
    private View imageView;
    private ImageGenerator imGen;
    private String tag = null;
    int width;

    public PhotoToLoad(String u, View i, int w) {
      url = u;
      imageView = i;
      width = w;
    }

    public PhotoToLoad(ImageGenerator iGen, String u, View i, int w) {
      url = u;
      imGen = iGen;
      imageView = i;
      width = w;
    }

    public synchronized View getImageView() {
      return imageView;
    }

    public String getTag() {
      if (tag == null) {
        if (isURL())
          tag = url;
        else
          tag = "imGen://" + imGen.getTag() + ":" + url;
      }
      return tag;
    }

    public Bitmap load() {
      // M.log("ImageLoader:PhotosLoader",
      // "PhotosLoader:run: loading bmp  " + getTag());
      File f = fileCache.getFile(getTag());

      // from SD cache
      Bitmap b = decodeFile(f, width);
      if (b != null)
        return b;

      if (isURL()) {
        b = downloadBitmap(f, url, width);
      } else {
        b = imGen.generateImage(url);
        OutputStream fos;
        try {
          fos = new FileOutputStream(f);
          b.compress(Bitmap.CompressFormat.PNG, 100, fos);
          fos.close();
        } catch (Exception e) {
          // TODO
          e.printStackTrace();
        }
      }
      return b;
    }

    private boolean isURL() {
      return imGen == null;
    }
  }

  class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;

    PhotosLoader(PhotoToLoad photoToLoad) {
      this.photoToLoad = photoToLoad;
    }

    @Override
    public void run() {
      // M.log("ImageLoader:PhotosLoader",
      // "PhotosLoader:run: attempting to get bmp  " +
      // photoToLoad.getTag());
      if (imageViewReused(photoToLoad))
        return;
      Bitmap bmp = photoToLoad.load();
      memoryCache.put(photoToLoad.getTag(), bmp);
      if (imageViewReused(photoToLoad))
        return;
      // M.log("ImageLoader:PhotosLoader",
      // "PhotosLoader:run: showing bmp " + photoToLoad.getTag());
      BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
      Activity a = (Activity) photoToLoad.getImageView().getContext();
      a.runOnUiThread(bd);
    }
  }

  boolean imageViewReused(PhotoToLoad photoToLoad) {
    String tag = imageViews.get(photoToLoad.getImageView());
    // M.log("ImageLoader", "imageViewReused: " + photoToLoad.getTag() + " "
    // + (tag == null ? "null" : tag));
    if (tag == null || !tag.equals(photoToLoad.getTag()))
      return true;
    return false;
  }

  // Used to display bitmap in the UI thread
  class BitmapDisplayer implements Runnable {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;

    public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
      bitmap = b;
      photoToLoad = p;
    }

    public void run() {
      if (imageViewReused(photoToLoad))
        return;
      View imageView = photoToLoad.getImageView();
      if (bitmap != null) {
        Drawable dr = new BitmapDrawable(res, bitmap);
        if (imageView instanceof ImageView)
          ((ImageView) imageView).setImageDrawable(dr);
        else
          imageView.setBackgroundDrawable(dr);
      } else {
        if (imageView instanceof ImageView)
          ((ImageView) imageView).setImageDrawable(mStub);
        else
          imageView.setBackgroundDrawable(mStub);
      }
    }
  }

  public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
    imageViews.clear();
  }

  public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {
      byte[] bytes = new byte[buffer_size];
      for (;;) {
        int count = is.read(bytes, 0, buffer_size);
        if (count == -1)
          break;
        os.write(bytes, 0, count);
      }
    } catch (Exception ex) {
    }
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java