文章目录
  1. 1. 数据类
  2. 2. 工具类
  3. 3. layout

数据类

  • Bimp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    public class Bimp {
    public static int max = 0;
    public static boolean act_bool = true;
    public static List<Bitmap> bmp = new ArrayList<Bitmap>();
    // 图片的存放地址
    public static List<String> drr = new ArrayList<String>();
    // 用于压缩图片
    public static Bitmap revitionImageSize(String path)throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
    new File(path)));
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, options);
    in.close();
    int i = 0;
    Bitmap bitmap = null;
    while (true) {
    if ((options.outWidth >> i <= 1000)
    && (options.outHeight >> i <= 1000)) {
    in = new BufferedInputStream(
    new FileInputStream(new File(path)));
    options.inSampleSize = (int) Math.pow(2.0D, i);
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeStream(in, null, options);
    break;
    }
    i++;
    }
    return bitmap;
    }
    }

工具类

  • BitmapCache类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    public class BitmapCache {
    public final String TAG = getClass().getSimpleName();
    public Handle h = new Handle();
    private HashMap<String,SoftReference<Bitmap>> imageCache = new HashMap<String,SoftReference<Bitmap>>();
    public void put(String path,Bitmap bmp) {
    if(!TextUtils.isEmpty(path) && bmp != null) {
    imageCache.put(path, new SoftReference<Bitmap>(bmp));
    }
    }
    public void displayBmp(final ImageView iv, final String thumbPath, final String sourcePath, final ImageCallback call ballback) {
    if (TextUtils.isEmpty(thumbPath) && TextUtils.isEmpty(sourcePath)) {
    Log.e(TAG, "no paths pass in");
    return;
    }
    final String path;
    final boolean isThumbPath;
    if (!TextUtils.isEmpty(thumbPath)) {
    path = thumbPath;
    isThumbPath = true;
    }else if (!TextUtils.isEmpty(sourcePath)) {
    path = sourcePath;
    isThumbPath = false;
    }else {
    return;
    }
    // 缓存中是否有该图片
    if(imageCache.containsKey(path)){
    SoftReference<Bitmap> reference = imageCache.get(path);
    if (bmp != null) {
    if (callback != null) {
    callback.imageLoad(iv, bmp ,sourcePath);
    }
    iv.setImageBitmap(bmp);
    Log.d(TAG,"hit cache");
    return;
    }
    }
    iv.setImageBitmap(null);
    new Thread() {
    Bitmap thumb;
    public void run(){
    try {
    if (isThumbPath) {
    thumb = BitmapFactory.decodeFile(thumbPath);
    if (thumb == null) {
    thumb = revitionImageSize(sourcePath);
    } else {
    thumb = revitionImageSize(thumbPath);
    }
    } catch (Exception e){

    }
    if (thumb == null) {
    thumb = revitionImageSize(sourcePath);
    }
    put(path,thumb);
    if(callback != null){
    h.post(new Runnable(){
    @Override
    public void run() {
    callback.imageLoad(iv, thumb, sourcePath);
    }
    });
    }
    }
    }.strat();
    }

    public Bitmap revitionImageSize(String path) throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
    new File(path)));
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, options);
    in.close();
    int i = 0;
    Bitmap bitmap = null;
    while (true) {
    if ((options.outWidth >> i <= 256)
    && (options.outHeight >> i <= 256)) {
    in = new BufferedInputStream(
    new FileInputStream(new File(path)));
    options.inSampleSize = (int) Math.pow(2.0D, i);
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeStream(in, null, options);
    break;
    }
    i += 1;
    }
    return bitmap;
    }

    public interface ImageCallback {
    public void imageLoad(ImageView imageView, Bitmap bitmap,
    Object... params);
    }
    }
  • AlbumHelper

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    public class AlbumHelper {
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private ContentResolver cr;

    // 缩略图列表
    public HashMap<String, String> thumbnailList = new HashMap<String, String>();
    // 专辑列表
    private List<HashMap<String, String>> albumList = new ArrayList<HashMap<String, String>>();
    private HashMap<String, ImageBucket> bucketList = new HashMap<String, ImageBucket>();

    private static AlbumHelper instance;

    private AlbumHelper() {
    }

    public static AlbumHelper getHelper() {
    if (instance == null) {
    instance = new AlbumHelper();
    }
    return instance;
    }

    /**
    * 初始化
    *
    * @param context
    */
    public void init(Context context) {
    if (this.context == null) {
    this.context = context;
    cr = context.getContentResolver();
    }
    }

    /**
    * 得到缩略图
    */
    private void getThumbnail() {
    String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID,
    Thumbnails.DATA };
    Cursor cursor = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection,
    null, null, null);
    getThumbnailColumnData(cursor);
    }

    /**
    * 从数据库中得到缩略图
    *
    * @param cur
    */
    private void getThumbnailColumnData(Cursor cur) {
    if (cur.moveToFirst()) {
    int _id;
    int image_id;
    String image_path;
    int _idColumn = cur.getColumnIndex(Thumbnails._ID);
    int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
    int dataColumn = cur.getColumnIndex(Thumbnails.DATA);

    do {
    _id = cur.getInt(_idColumn);
    image_id = cur.getInt(image_idColumn);
    image_path = cur.getString(dataColumn);
    thumbnailList.put("" + image_id, image_path);
    } while (cur.moveToNext());
    }
    }

    /**
    * 得到原图
    */
    private void getAlbum() {
    String[] projection = { Albums._ID, Albums.ALBUM, Albums.ALBUM_ART,
    Albums.ALBUM_KEY, Albums.ARTIST, Albums.NUMBER_OF_SONGS };
    Cursor cursor = cr.query(Albums.EXTERNAL_CONTENT_URI, projection, null,
    null, null);
    getAlbumColumnData(cursor);

    }

    /**
    * 从本地数据库中得到原图
    *
    * @param cur
    */
    private void getAlbumColumnData(Cursor cur) {
    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 {
    _id = cur.getInt(_idColumn);
    album = cur.getString(albumColumn);
    albumArt = cur.getString(albumArtColumn);
    albumKey = cur.getString(albumKeyColumn);
    artist = cur.getString(artistColumn);
    numOfSongs = cur.getInt(numOfSongsColumn);

    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 + "");
    albumList.add(hash);
    } while (cur.moveToNext());
    }
    }

    /**
    * 是否创建了图片集
    */
    private boolean hasBuildImagesBucketList = false;

    /**
    * 得到图片集
    */
    private void buildImagesBucketList() {
    long startTime = System.currentTimeMillis();

    // 构造缩略图索引
    getThumbnail();

    // 构造相册索引
    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 };
    // 得到一个游标
    Cursor cur = cr.query(Media.EXTERNAL_CONTENT_URI, columns, null, null,
    null);
    if (cur.moveToFirst()) {
    // 获取指定列的索引
    int photoIDIndex = cur.getColumnIndexOrThrow(Media._ID);
    int photoPathIndex = cur.getColumnIndexOrThrow(Media.DATA);
    int photoNameIndex = cur.getColumnIndexOrThrow(Media.DISPLAY_NAME);
    int photoTitleIndex = cur.getColumnIndexOrThrow(Media.TITLE);
    int photoSizeIndex = cur.getColumnIndexOrThrow(Media.SIZE);
    int bucketDisplayNameIndex = cur
    .getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME);
    int bucketIdIndex = cur.getColumnIndexOrThrow(Media.BUCKET_ID);
    int picasaIdIndex = cur.getColumnIndexOrThrow(Media.PICASA_ID);
    // 获取图片总数
    int totalNum = cur.getCount();

    do {
    String _id = cur.getString(photoIDIndex);
    String name = cur.getString(photoNameIndex);
    String path = cur.getString(photoPathIndex);
    String title = cur.getString(photoTitleIndex);
    String size = cur.getString(photoSizeIndex);
    String bucketName = cur.getString(bucketDisplayNameIndex);
    String bucketId = cur.getString(bucketIdIndex);
    String picasaId = cur.getString(picasaIdIndex);

    Log.i(TAG, _id + ", bucketId: " + bucketId + ", picasaId: "
    + picasaId + " name:" + name + " path:" + path
    + " title: " + title + " size: " + size + " bucket: "
    + bucketName + "---");

    ImageBucket bucket = bucketList.get(bucketId);
    if (bucket == null) {
    bucket = new ImageBucket();
    bucketList.put(bucketId, bucket);
    bucket.imageList = new ArrayList<ImageItem>();
    bucket.bucketName = bucketName;
    }
    bucket.count++;
    ImageItem imageItem = new ImageItem();
    imageItem.imageId = _id;
    imageItem.imagePath = path;
    imageItem.thumbnailPath = thumbnailList.get(_id);
    bucket.imageList.add(imageItem);

    } while (cur.moveToNext());
    }
    // Interator为迭代器,Entry为Map接口
    // 遍历所有图片
    Iterator<Entry<String, ImageBucket>> itr = bucketList.entrySet()
    .iterator();
    while (itr.hasNext()) {
    Map.Entry<String, ImageBucket> entry = (Map.Entry<String, ImageBucket>) itr
    .next();
    ImageBucket bucket = entry.getValue();
    Log.d(TAG, entry.getKey() + ", " + bucket.bucketName + ", "
    + bucket.count + " ---------- ");
    for (int i = 0; i < bucket.imageList.size(); ++i) {
    ImageItem image = bucket.imageList.get(i);
    Log.d(TAG, "----- " + image.imageId + ", " + image.imagePath
    + ", " + image.thumbnailPath);
    }
    }
    hasBuildImagesBucketList = true;
    long endTime = System.currentTimeMillis();
    Log.d(TAG, "use time: " + (endTime - startTime) + " ms");
    }

    /**
    * 得到图片集
    *
    * @param refresh
    * @return
    */
    public List<ImageBucket> getImagesBucketList(boolean refresh) {
    if (refresh || (!refresh && !hasBuildImagesBucketList)) {
    buildImagesBucketList();
    }
    List<ImageBucket> tmpList = new ArrayList<ImageBucket>();
    Iterator<Entry<String, ImageBucket>> itr = bucketList.entrySet()
    .iterator();
    while (itr.hasNext()) {
    Map.Entry<String, ImageBucket> entry = (Map.Entry<String, ImageBucket>) itr
    .next();
    tmpList.add(entry.getValue());
    }
    return tmpList;
    }

    /**
    * 根据图片的_ID得到原始图像路径
    *
    * @param image_id
    * @return
    */
    public String getOriginalImagePath(String image_id) {
    String path = null;
    Log.i(TAG, "---(^o^)----" + image_id);
    String[] projection = { Media._ID, Media.DATA };
    Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, projection,
    Media._ID + "=" + image_id, null, null);
    if (cursor != null) {
    cursor.moveToFirst();
    path = cursor.getString(cursor.getColumnIndex(Media.DATA));
    }
    return path;
    }
    }

layout

相册选取layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="# f1eff5"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="# ff495a" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="相册"
android:textColor="# ffffff"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="@drawable/text_while"
android:text="取消"
android:textSize="20sp" />
</RelativeLayout>
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:horizontalSpacing="20dp"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="20dp" >
</GridView>
</LinearLayout>

  • 图片选取layout

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="# f1eff5"
    android:orientation="vertical" >
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="# ff495a" >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="相册"
    android:textColor="# ffffff"
    android:textSize="20sp" />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:textColor="@drawable/text_while"
    android:text="取消"
    android:textSize="20sp" />
    </RelativeLayout>
    <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:horizontalSpacing="20dp"
    android:numColumns="2"
    android:scrollbars="none"
    android:verticalSpacing="20dp" >
    </GridView>
    </LinearLayout>
  • 图片预览layout

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>
    <RelativeLayout
    android:id="@+id/photo_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true" >
    <Button
    android:id="@+id/photo_bt_exit"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/bt_quxiao" />
    <Button
    android:id="@+id/photo_bt_del"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerInParent="true"
    android:background="@drawable/bt_shanchu" />
    <Button
    android:id="@+id/photo_bt_enter"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="@drawable/bt_queding" />
    </RelativeLayout>
    </RelativeLayout>
文章目录
  1. 1. 数据类
  2. 2. 工具类
  3. 3. layout