然后,我们就可以在Activity中通过查询MediaStore的多媒体图片库来查询所有的图片的缩略图,缩略图所在的位置是:
MediaStore.Images.Thumbnails。Activity代码如下:
package com.liner.manager; import java.util.ArrayList; import java.util.List; import com.liner.manager.adapter.GridImageAdapter; import Android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends Activity { private GridView photoView; private GridImageAdapter imageAdapter; private Cursor cursor; private int[] thumbIds; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); photoView = (GridView)this.findViewById(R.id.view_photos); photoView.setOnItemClickListener(photoClickListener); //showImages(); showThumbnails(); } private void showThumbnails(){ cursor = BitmapUtils.queryThumbnails(this); if(cursor.moveToFirst()){ List<Bitmap> bitmaps = new ArrayList<Bitmap>(); thumbIds = new int[cursor.getCount()]; for(int i=0; i<cursor.getCount();i++){ cursor.moveToPosition(i); String currPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); thumbIds[i] = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID)); Bitmap b = BitmapUtils.decodeBitmap(currPath,100,100); bitmaps.add(b); } imageAdapter = new GridImageAdapter(this.getApplication(), bitmaps); photoView.setAdapter(imageAdapter); } } private AdapterView.OnItemClickListener photoClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> p, View v, int position, long id) { //点击某张缩略图时,转到图片显示界面 Intent intent = new Intent(); intent.setClass(MainActivity.this, GalleryActivity.class); intent.putExtra("thumbIds", thumbIds); intent.putExtra("currentPos", position); startActivity(intent); } }; }注意到,我们记录了,所有缩略图对应的id号,和当前的用户选择的位置,然后通过Intent传递到第二个展示界面。第二个界面的布局文件如下:我们用了一个Gallery和一个ImageButton来实现
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Gallery android:id="@+id/image_gallery" android:layout_width="fill_parent" android:layout_height="100dp" /> <ImageButton android:id="@+id/image_current" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:layout_marginTop="10dp" /> </LinearLayout>然后,对应的Activity如下:
package com.liner.manager; import java.util.List; import com.liner.manager.adapter.GridImageAdapter; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageButton; import android.widget.AdapterView.OnItemClickListener; public class GalleryActivity extends Activity{ private ImageButton currentImage; private Gallery gallery; private int[] thumbIds; private int currentPos; private Bitmap currentBitmap; private List<Bitmap> bitmapCache; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.gallery); currentImage = (ImageButton)this.findViewById(R.id.image_current); gallery = (Gallery)this.findViewById(R.id.image_gallery); gallery.setOnItemClickListener(galleryItemClickListener); init(); } private OnItemClickListener galleryItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> p, View v, int position, long id) { // 点击事件 showCurrentImage(position); } }; private void init(){ thumbIds = this.getIntent().getIntArrayExtra("thumbIds"); currentPos = this.getIntent().getIntExtra("currentPos",0); //galleryIds = this.getThumbnailIds(currentPos); //当前的gallery里的图片信息 bitmapCache = BitmapUtils.queryThumbnailListByIds(this, thumbIds); GridImageAdapter adapter = new GridImageAdapter(this.getApplication(), bitmapCache); gallery.setAdapter(adapter); gallery.setSelection(currentPos); showCurrentImage(currentPos); } private void showCurrentImage(int position){ if(currentBitmap != null){ currentBitmap.recycle(); } currentBitmap = BitmapUtils.queryImageByThumbnailId(GalleryActivity.this, thumbIds[position]); if(currentBitmap != null){ currentImage.setImageBitmap(currentBitmap); }else{ //什么都不做 } //releaseBitmaps(); } }