Android Open Source - Alfred4Android Thumbnail Util






From Project

Back to project page Alfred4Android.

License

The source code is released under:

Apache License

If you think the Android project Alfred4Android 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.toraleap.collimator.util;
/*from w  ww  .  j a va2  s  . c  o m*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * ???????????????????????????????????????????????????????????????????????????????????? 96x96??
 * @author    uestc.Mobius <mobius@toraleap.com>
 * @version  2010.1029
 */
public class ThumbnailUtil extends SoftCache<String, Bitmap> {

  private Bitmap mDefaultThumbnail;
  private Bitmap mLoadingThumbnail;
  private Context mContext;
  private SharedPreferences mPrefs;
  private boolean isDisplayThumbnail;
  
  /**
   * ???????????????????
   * @param context  ??????????(?????? Activity ?????????????? getApplicationContext() ???)
   */
  public ThumbnailUtil(Context context, Handler handler) {
    super(handler);
    mContext = context;
    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    refresh();
  }
  
  /**
   * ???????????????
   */
  public void refresh() {
    isDisplayThumbnail = mPrefs.getBoolean("display_thumbnail", true);
  }

  @Override
  Bitmap request(String key) {
    return fromFile(key);
  }
  
  @Override
  int getMaxQueueLength() {
    return 10;
  }
  
  @Override
  Bitmap getDefault() {
    if (null == mLoadingThumbnail) {
      try {
        InputStream is = mContext.getAssets().open("icons/loading.png");
        mLoadingThumbnail = BitmapFactory.decodeStream(is);
        is.close();
      } catch (IOException e) {
        mLoadingThumbnail = getUndefined();
      }
    }
    return mLoadingThumbnail;
  }
  
  /**
   * ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
   * @param filename  ?????????????
   * @return ???????
   */
  private Bitmap fromFile(String filename) {
    Bitmap thumbnail = null;
    if (isDisplayThumbnail) {
      String mimeType = FileInfo.mimeType(filename);
      if (mimeType.startsWith("image/")) {
        thumbnail = loadFromImage(filename);
      } else if (mimeType.startsWith("video/")) {
        thumbnail = loadFromVideo(filename);
      } else if (mimeType.startsWith("audio/")) {
        thumbnail = loadFromAudio(filename);
      } else if (mimeType.startsWith("text/plain")) {
        thumbnail = loadFromAudio(filename);
      } else if (mimeType.equals("application/vnd.android.package-archive")) {
        thumbnail = loadFromApk(filename);
      }
    }
    return (null == thumbnail) ? fromExt(filename) : thumbnail;
  }
  
  /**
   * ????????????????????????????????
   * @param path  ?????????????
   * @return   ???????
   */
  private Bitmap loadFromImage(String path) {
    Bitmap thumbnail = null;
    try{
      Options options = new Options();
      // ????????????????
      options.inSampleSize = 1;
          options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);
          if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) return fromExt(path);
          // ???????????????????
          setSampleSize(options);
          options.inDither = true;
          options.inJustDecodeBounds = false;
          //options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      Bitmap bitmap = BitmapFactory.decodeFile(path, options);
      if (null != bitmap) {
        thumbnail = scaleBitmap(bitmap);
        bitmap.recycle();
        bitmap = null;
      }
      return thumbnail;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
  /**
   * ?????????????????????????????????????????????
   * @param path  ?????????????
   * @return   ???????
   */
  private Bitmap loadFromAudio(String path) {
    String folder = path.substring(0, path.lastIndexOf("/"));
    if (new File(folder + "/AlbumArt.jpg").exists()) {
      return requestAndCache(folder + "/AlbumArt.jpg");
    } else if (new File(folder + "/cover.jpg").exists()) {
      return requestAndCache(folder + "/cover.jpg");
    } else if (new File(folder + "/folder.jpg").exists()) {
      return requestAndCache(folder + "/folder.jpg");
    }
    return null;
  }
  
  /**
   * ???????????????????????????????
   * @param path  ?????????????
   * @return   ???????
   */
  private Bitmap loadFromVideo(String path) {
    Bitmap thumbnail = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
            retriever.setDataSource(path);
            Bitmap bitmap = retriever.captureFrame();
            thumbnail = scaleBitmap(bitmap);
      bitmap.recycle();
      bitmap = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } catch (NoSuchMethodError err) {
          err.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                ex.printStackTrace();
            }
        }
    return thumbnail;
    }
  
  /**
   * ??????????????????????????
   * @param path  ?????????????
   * @return   ???????
   */
  private Bitmap loadFromApk(String path) {
    Bitmap thumbnail = null;
    PackageManager pm = mContext.getPackageManager();      
        PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);      
        if(info != null){
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.sourceDir = path;
            appInfo.publicSourceDir = path;
            Drawable drawable = pm.getApplicationIcon(appInfo);
            thumbnail = scaleBitmap(((BitmapDrawable)drawable).getBitmap());
        }
    return thumbnail;
  }

  /**
   * ????????????????????????????????????????????????????????????????????
   * @param filename  ?????
   * @return   ???????
   */
  private Bitmap fromExt(String filename) {
    String ext = FileInfo.extension(filename);
    Bitmap thumbnail = getCache(ext);
    if (thumbnail != null) return thumbnail;
    try {
      InputStream is = mContext.getAssets().open("icons/ext/" + ext + ".png");
      thumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
      is.close();
    } catch (IOException e) {
      try {
        String mime = FileInfo.mimeType(filename).replace('/', '_');
        InputStream is = mContext.getAssets().open("icons/mime/" + mime + ".png");
        thumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
        is.close();
      } catch (IOException ex) {
        try {
          String mimetype = FileInfo.mimeType(filename).split("/")[0];
          InputStream is = mContext.getAssets().open("icons/mime/" + mimetype + ".png");
          thumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
          is.close();
        } catch (IOException exc) {
          exc.printStackTrace();
          thumbnail = getUndefined();
        }
      }
    }
    return putCache(ext, thumbnail);
  }
  
  /**
   * ????????????????????
   * @return  ???????
   */
  public Bitmap getUndefined() {
    if (null == mDefaultThumbnail) {
      try {
        InputStream is = mContext.getAssets().open("icons/undefined.png");
        mDefaultThumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
        is.close();
      } catch (IOException e) {
        mDefaultThumbnail = null;
      }
    }
    return mDefaultThumbnail;
  }

  private static void setSampleSize(Options options) {
    int maxSize = options.outWidth > options.outHeight ? options.outWidth : options.outHeight;
    int minSample = (maxSize / 96) >> 1 ;
    int sample = 1;
    while (sample <= minSample) sample <<= 1;
    options.inSampleSize = sample;
  }
  
  private static Bitmap scaleBitmap(Bitmap source) {
    int maxSize = source.getWidth() > source.getHeight() ? source.getWidth() : source.getHeight();
        return Bitmap.createScaledBitmap(source, source.getWidth() * 96 / maxSize, source.getHeight() * 96 / maxSize, true);
  }
}




Java Source Code List

android.media.MediaMetadataRetriever.java
com.toraleap.collimator.AppSearchActivity.java
com.toraleap.collimator.GlobalContext.java
com.toraleap.collimator.HelpActivity.java
com.toraleap.collimator.PrefsActivity.java
com.toraleap.collimator.SearchActivity.java
com.toraleap.collimator.ShortcutActivity.java
com.toraleap.collimator.bll.FileScannerService.java
com.toraleap.collimator.bll.TagGenerator.java
com.toraleap.collimator.dal.DBColumns.java
com.toraleap.collimator.dal.DBHelper.java
com.toraleap.collimator.dal.DBOperation.java
com.toraleap.collimator.data.Expression.java
com.toraleap.collimator.data.IndexData.java
com.toraleap.collimator.data.IndexLoader.java
com.toraleap.collimator.data.Index.java
com.toraleap.collimator.data.Match.java
com.toraleap.collimator.data.Matcher.java
com.toraleap.collimator.data.Sorter.java
com.toraleap.collimator.ext.GlobalProvider.java
com.toraleap.collimator.ext.Playlist.java
com.toraleap.collimator.model.BaseTag.java
com.toraleap.collimator.ui.FloatingDialog.java
com.toraleap.collimator.ui.MatchAdapter.java
com.toraleap.collimator.util.DigestUtil.java
com.toraleap.collimator.util.FileInfo.java
com.toraleap.collimator.util.MimeTypeMap.java
com.toraleap.collimator.util.RecursiveFileObserver.java
com.toraleap.collimator.util.ShortcutHelper.java
com.toraleap.collimator.util.SoftCache.java
com.toraleap.collimator.util.ThumbnailUtil.java
com.toraleap.collimator.util.Unicode2Alpha.java