Android Open Source - UniversalImagePick Images Helper






From Project

Back to project page UniversalImagePick.

License

The source code is released under:

Apache License

If you think the Android project UniversalImagePick 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.luffyjet.universalimagepick.model;
//  w w  w .jav a  2  s  .  c  om
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;

import com.luffyjet.universalimagepick.utils.LogUtil;

/**
 * ?????????????
 * 
 * @author Luffyjet
 * @version 1.0.0
 * @since 2014-9-15 {@link https://github.com/luffyjet}
 */
public class ImagesHelper {
  private static final String TAG = "ImagesHelper";
  /**
   * the ImagesHelper instance
   */
  private static ImagesHelper instance;
  /**
   * ContentResolver
   */
  private ContentResolver mContentResolver;
  /**
   * SD???????
   */
  public int mTotalCount;
  /**
   * ????????
   */
  private static ArrayList<Image> mAllImageList = new ArrayList<Image>();
  /**
   * ???Map????
   */
  private static HashMap<String, Thumbnail> mThumbnailMap = new HashMap<String, Thumbnail>();
  /**
   * ????Map????
   */
  private static HashMap<String, ImageBucket> mBucketMap = new HashMap<String, ImageBucket>();
  /**
   * ?????????????????
   */
  boolean isInitedData = false;

  private ImagesHelper(Context context) {
    mContentResolver = context.getContentResolver();
  }

  public static ImagesHelper getHelper(Context context) {
    if (instance == null) {
      instance = new ImagesHelper(context);
    }
    return instance;
  }

  /**
   * ??????? Get Thumbnails
   */
  private void getThumbnails() {
    String[] columns = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA };
    Cursor cursor = mContentResolver.query(Thumbnails.EXTERNAL_CONTENT_URI, columns, null, null, null);
    if (null != cursor) {
      try {
        while (cursor.moveToNext()) {
          String id = cursor.getString(cursor.getColumnIndex(Thumbnails._ID));
          String imageId = cursor.getString(cursor.getColumnIndex(Thumbnails.IMAGE_ID));
          String imagePath = cursor.getString(cursor.getColumnIndex(Thumbnails.DATA));
          Thumbnail thumbnail = new Thumbnail(id, imageId, imagePath);
          mThumbnailMap.put(imageId, thumbnail);
        }
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        cursor.close();
      }
    }
  }

  /**
   * Get Images
   * 
   * @param handler
   * @param selection
   * @param selectionArgs
   * @param sortOrder
   */
  private void getImages(Handler handler, String selection, String[] selectionArgs, String sortOrder) {
    long startTime = System.currentTimeMillis();

    // ?????????? ??????????? ?????????????
    clear();

    // ???????
    getThumbnails();

    // ??
    String columns[] = new String[] { Media._ID, Media.BUCKET_ID, Media.PICASA_ID, Media.DATA, Media.DISPLAY_NAME, Media.TITLE, Media.SIZE, Media.BUCKET_DISPLAY_NAME, Media.DATE_MODIFIED, Media.DATE_ADDED };
    // ????
    Cursor cursor = mContentResolver.query(Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, sortOrder);

    if (null != cursor) {
      try {
        // ????????
        mTotalCount = cursor.getCount();
        // ????????
        while (cursor.moveToNext()) {
          String id = cursor.getString(cursor.getColumnIndexOrThrow(Media._ID));
          String path = cursor.getString(cursor.getColumnIndexOrThrow(Media.DATA));
          String name = cursor.getString(cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME));
          String title = cursor.getString(cursor.getColumnIndexOrThrow(Media.TITLE));
          String size = cursor.getString(cursor.getColumnIndexOrThrow(Media.SIZE));
          String bucketName = cursor.getString(cursor.getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME));
          String bucketId = cursor.getString(cursor.getColumnIndexOrThrow(Media.BUCKET_ID));
          String picasaId = cursor.getString(cursor.getColumnIndexOrThrow(Media.PICASA_ID));
          String dateModified = cursor.getString(cursor.getColumnIndexOrThrow(Media.DATE_MODIFIED));
          String dateAdded = cursor.getString(cursor.getColumnIndexOrThrow(Media.DATE_ADDED));

          LogUtil.i(TAG, "id:" + id + ", bucketId: " + bucketId + ", picasaId: " + picasaId + " name:" + name + " path:" + path + " title: " + title + " size: " + size + " bucket: " + bucketName + " addtime:" + dateAdded + " modified:" + dateModified);
          /**
           * ????Bucket??Image
           */
          // ????id?Map??????????
          ImageBucket bucket = mBucketMap.get(bucketId);
          if (bucket == null) {
            // ??????????Map
            bucket = new ImageBucket();
            mBucketMap.put(bucketId, bucket);
            bucket.bucketId = bucketId;
            bucket.bucketName = bucketName;
          }
          // ???????
          Image image = new Image(id, name, path, title, size, bucketName, bucketId, picasaId, dateModified, dateAdded);
          if (null != mThumbnailMap.get(id)) {
            image.thumbnailPath = mThumbnailMap.get(id).thumbnailPath;
          }
          // ????????
          bucket.addImage(image);
          // ??????????
          mAllImageList.add(image);

          // ???????? ??????
          if (null != handler) {
            Message msg = handler.obtainMessage();
            msg.obj = image;
            handler.sendMessage(msg);
          }
        }
        // ???????????
        isInitedData = true;
      } catch (Exception e) {
        e.printStackTrace();
        // ??????
        isInitedData = false;
      } finally {
        cursor.close();
      }
    }

    long endTime = System.currentTimeMillis();
    Log.d(TAG, "get data use time: " + (endTime - startTime) + " ms");
  }

  /**
   * 
   * ??????????
   * 
   * @param handler
   *            android.os.Handler
   * @param isNeedRefresh
   *            ??????????????????
   * @param selection
   * @param selectionArgs
   * @param sortOrder
   * @return
   */
  public ArrayList<ImageBucket> getImagesBucketList(Handler handler, String selection, String[] selectionArgs, String sortOrder, boolean isNeedRefresh) {
    // ????? ????????????????????
    if (isNeedRefresh || !isInitedData) {
      getImages(handler, selection, selectionArgs, sortOrder);
    }
    ArrayList<ImageBucket> buckets = new ArrayList<ImageBucket>();
    buckets.addAll(mBucketMap.values());

    // ?? ?????????
    for (ImageBucket bucket : buckets) {
      LogUtil.i(TAG, bucket.toString());
    }

    return buckets;
  }

  /**
   * ??????????
   * 
   * @return
   */
  public ArrayList<Image> getAllImages() {
    return mAllImageList;
  }

  /**
   * ??????Id??????
   * 
   * @param image_id
   * @return
   */
  String getSourcePathById(String image_id) {
    String path = null;
    String[] projection = { Media._ID, Media.DATA };
    Cursor cursor = mContentResolver.query(Media.EXTERNAL_CONTENT_URI, projection, Media._ID + "=" + image_id, null, null);
    if (cursor != null) {
      try {
        cursor.moveToFirst();
        path = cursor.getString(cursor.getColumnIndex(Media.DATA));
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        cursor.close();
      }
    }
    return path;
  }

  /**
   * ????????????
   */
  public void clear() {
    mAllImageList.clear();
    mThumbnailMap.clear();
    mBucketMap.clear();
    // AudioAlbumList.clear();
  }

  // /**
  // * ??????
  // */
  // private static List<HashMap<String, String>> AudioAlbumList = new
  // ArrayList<HashMap<String, String>>();
  // /**
  // * ??????
  // */
  // public void getAlbum() {
  // String[] projection = { Albums._ID, Albums.ALBUM, Albums.ALBUM_ART,
  // Albums.ALBUM_KEY, Albums.ARTIST, Albums.NUMBER_OF_SONGS };
  // Cursor cur = mContentResolver.query(Albums.EXTERNAL_CONTENT_URI,
  // projection, null, null, null);
  //
  // if (cur.moveToFirst()) {
  // int _id;
  // String album;
  // String albumArt;
  // String albumKey;
  // String artist;
  // int numOfSongs;
  //
  // int _idColumn = cur.getColumnIndex(Albums._ID);
  // int albumColumn = cur.getColumnIndex(Albums.ALBUM);
  // int albumArtColumn = cur.getColumnIndex(Albums.ALBUM_ART);
  // int albumKeyColumn = cur.getColumnIndex(Albums.ALBUM_KEY);
  // int artistColumn = cur.getColumnIndex(Albums.ARTIST);
  // int numOfSongsColumn = cur.getColumnIndex(Albums.NUMBER_OF_SONGS);
  //
  // do {
  // // Get the field values
  // _id = cur.getInt(_idColumn);
  // album = cur.getString(albumColumn);
  // albumArt = cur.getString(albumArtColumn);
  // albumKey = cur.getString(albumKeyColumn);
  // artist = cur.getString(artistColumn);
  // numOfSongs = cur.getInt(numOfSongsColumn);
  //
  // // Do something with the values.
  // Log.i(TAG, _id + " album:" + album + " albumArt:" + albumArt +
  // "albumKey: " + albumKey + " artist: " + artist + " numOfSongs: " +
  // numOfSongs + "---");
  // HashMap<String, String> hash = new HashMap<String, String>();
  // hash.put("_id", _id + "");
  // hash.put("album", album);
  // hash.put("albumArt", albumArt);
  // hash.put("albumKey", albumKey);
  // hash.put("artist", artist);
  // hash.put("numOfSongs", numOfSongs + "");
  // AudioAlbumList.add(hash);
  //
  // } while (cur.moveToNext());
  //
  // }
  // }

}




Java Source Code List

com.luffyjet.universalimagepick.App.java
com.luffyjet.universalimagepick.Constants.java
com.luffyjet.universalimagepick.MainActivity.java
com.luffyjet.universalimagepick.Test.java
com.luffyjet.universalimagepick.adapter.BucketAdapter.java
com.luffyjet.universalimagepick.adapter.GalleryAdapter.java
com.luffyjet.universalimagepick.adapter.PickGridAdapter.java
com.luffyjet.universalimagepick.adapter.PreviewAdapter.java
com.luffyjet.universalimagepick.adapter.ResultAdapter.java
com.luffyjet.universalimagepick.model.ImageBucket.java
com.luffyjet.universalimagepick.model.Image.java
com.luffyjet.universalimagepick.model.ImagesHelper.java
com.luffyjet.universalimagepick.model.Thumbnail.java
com.luffyjet.universalimagepick.ui.BaseActivity.java
com.luffyjet.universalimagepick.ui.GalleryActivity.java
com.luffyjet.universalimagepick.ui.PickActivity.java
com.luffyjet.universalimagepick.utils.LogUtil.java
com.luffyjet.universalimagepick.widget.CropImageView.java
com.luffyjet.universalimagepick.widget.CustomGridView.java
com.luffyjet.universalimagepick.widget.MulitPointTouchListener.java
com.luffyjet.universalimagepick.widget.ViewfinderView.java