![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
這篇文章,咱們將學習如何檢索並顯示媒體庫中的圖片以及每張圖片的詳細信息包括名稱,ID,路徑,大小等等。java
關於遊標(cursor)不懂的能夠看博文:Android中Cursor類的概念和用法android
具體實現:app
- 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>