这篇文章,我们将学习如何检索并显示媒体库中的图片以及每张图片的详细信息包括名称,ID,路径,大小等等。
关于游标(cursor)不懂的可以看博文:Android中Cursor类的概念和用法
具体实现:
- package xiaosi.photoLibrary;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.provider.MediaStore.Images.Media;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- /**
- * 该类完成图片的检索,显示功能
- *
- * @author Administrator
- *
- */
- public class PhotoLibraryActivity extends Activity implements OnClickListener
- {
- private ImageView photo;
- private Button next = null;
- private Button previous = null;
- private Button message = null;
- private TextView position = null;
- private Cursor cursor;
- private int photoIndex;
- private int photoNameIndex;
- private int photoIDIndex;
- private int photoTitleIndex;
- private int photoSizeIndex;
- private String Message = null;
- private int totalNum = 0;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init()
- {
- next = (Button) findViewById(R.id.next);
- next.setOnClickListener(this);
- previous = (Button) findViewById(R.id.previous);
- previous.setOnClickListener(this);
- message = (Button) findViewById(R.id.message);
- message.setOnClickListener(this);
- photo = (ImageView) this.findViewById(R.id.image_view);
- position = (TextView) findViewById(R.id.number);
- // 指定获取的列
- String columns[] = new String[] { Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME, Media.SIZE };
- // 得到一个游标
- cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
- // 获取指定列的索引
- photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);
- photoNameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
- photoIDIndex = cursor.getColumnIndexOrThrow(Media._ID);
- photoTitleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);
- photoSizeIndex = cursor.getColumnIndexOrThrow(Media.SIZE);
- // 获取图片总数
- totalNum = cursor.getCount();
- // 跳到第一个图片
- if (cursor.moveToFirst())
- {
- setImage();
- position.setText("(1/" + totalNum + ")");
- }
- }
- @Override
- public void onClick(View arg0)
- {
- switch (arg0.getId())
- {
- // 下一个
- case R.id.next:
- if (cursor.moveToNext())
- {
- setImage();
- }
- else
- {
- cursor.moveToLast();
- }
- break;
- // 上一个
- case R.id.previous:
- if (cursor.moveToPrevious())
- {
- setImage();
- }
- else
- {
- cursor.moveToFirst();
- }
- break;
- case R.id.message:
- // Dialog显示详细信息
- AlertDialog.Builder builder = new AlertDialog.Builder(PhotoLibraryActivity.this);
- builder.setTitle("详细信息");
- builder.setMessage(Message);
- builder.setPositiveButton("关闭", new android.content.DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which)
- {
- dialog.dismiss();
- }
- });
- builder.show();
- break;
- }
- }
- private void setImage()
- {
- // 获取图片的Name
- String name = cursor.getString(photoNameIndex);
- // 获取图片的ID
- String number = cursor.getString(photoIDIndex);
- // 获取图片的Title
- String title = cursor.getString(photoTitleIndex);
- // 获取图片的大小
- String size = cursor.getString(photoSizeIndex);
- // 获取图片存储路径
- String path = cursor.getString(photoIndex);
- // 为TextView:position赋值(现在所在的位置)
- position.setText("(" + number + "/" + totalNum + ")");
- Message = "Name:" + name + "\n" + "Number:" + number + "\n" + "Title:" + title + "\n" + "Size:" + size + "\n" + "Path:" + path;
- // 通过路径获取图片
- Drawable image = Drawable.createFromPath(path);
- photo.setImageDrawable(image);
- }
- }
mian.xml
- <?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:background="@drawable/background"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/next"
- android:layout_width="60dip"
- android:layout_height="50dip"
- android:background="@drawable/next"
- android:paddingLeft="30dip" />
- <TextView
- android:id="@+id/number"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:paddingLeft="30dip"
- android:textColor="#000000"/>
- <Button
- android:id="@+id/previous"
- android:layout_width="60dip"
- android:layout_height="50dip"
- android:background="@drawable/previous"
- android:paddingLeft="30dip" />
- <Button
- android:id="@+id/message"
- android:layout_width="80dip"
- android:layout_height="40dip"
- android:paddingLeft="30dip"
- android:text="详情"/>
- </LinearLayout>
- <ImageView
- android:id="@+id/image_view"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>