本文共 3731 字,大约阅读时间需要 12 分钟。
这篇文章,我们将学习如何检索并显示媒体库中的图片以及每张图片的详细信息包括名称,ID,路径,大小等等。
关于游标(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
一起见证菜鸟的起飞。。。。。。
转载地址:http://jodsi.baihongyu.com/