检索Android图片库并显示详细信息


这篇文章,我们将学习如何检索并显示媒体库中的图片以及每张图片的详细信息包括名称,ID,路径,大小等等。

关于游标(cursor)不懂的可以看博文:Android中Cursor类的概念和用法

具体实现:


[java] view plain copy
  1. package xiaosi.photoLibrary;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.database.Cursor;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.drawable.Drawable;  
  9. import android.os.Bundle;  
  10. import android.provider.MediaStore.Images.Media;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16.   
  17. /** 
  18.  * 该类完成图片的检索,显示功能 
  19.  *  
  20.  * @author Administrator 
  21.  *  
  22.  */  
  23. public class PhotoLibraryActivity extends Activity implements OnClickListener  
  24. {  
  25.     private ImageView   photo;  
  26.     private Button      next        = null;  
  27.     private Button      previous    = null;  
  28.     private Button      message     = null;  
  29.     private TextView    position    = null;  
  30.     private Cursor      cursor;  
  31.     private int         photoIndex;  
  32.     private int         photoNameIndex;  
  33.     private int         photoIDIndex;  
  34.     private int         photoTitleIndex;  
  35.     private int         photoSizeIndex;  
  36.     private String      Message     = null;  
  37.     private int         totalNum    = 0;  
  38.   
  39.     public void onCreate(Bundle savedInstanceState)  
  40.     {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.main);  
  43.         init();  
  44.     }  
  45.   
  46.     private void init()  
  47.     {  
  48.         next = (Button) findViewById(R.id.next);  
  49.         next.setOnClickListener(this);  
  50.         previous = (Button) findViewById(R.id.previous);  
  51.         previous.setOnClickListener(this);  
  52.         message = (Button) findViewById(R.id.message);  
  53.         message.setOnClickListener(this);  
  54.         photo = (ImageView) this.findViewById(R.id.image_view);  
  55.         position = (TextView) findViewById(R.id.number);  
  56.         // 指定获取的列  
  57.         String columns[] = new String[] { Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME, Media.SIZE };  
  58.         // 得到一个游标  
  59.         cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);  
  60.         // 获取指定列的索引  
  61.         photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);  
  62.         photoNameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);  
  63.         photoIDIndex = cursor.getColumnIndexOrThrow(Media._ID);  
  64.         photoTitleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);  
  65.         photoSizeIndex = cursor.getColumnIndexOrThrow(Media.SIZE);  
  66.         // 获取图片总数  
  67.         totalNum = cursor.getCount();  
  68.         // 跳到第一个图片  
  69.         if (cursor.moveToFirst())  
  70.         {  
  71.             setImage();  
  72.             position.setText("(1/" + totalNum + ")");  
  73.         }  
  74.     }  
  75.   
  76.     @Override  
  77.     public void onClick(View arg0)  
  78.     {  
  79.         switch (arg0.getId())  
  80.         {  
  81.         // 下一个  
  82.         case R.id.next:  
  83.             if (cursor.moveToNext())  
  84.             {  
  85.                 setImage();  
  86.             }  
  87.             else  
  88.             {  
  89.                 cursor.moveToLast();  
  90.             }  
  91.             break;  
  92.         // 上一个  
  93.         case R.id.previous:  
  94.             if (cursor.moveToPrevious())  
  95.             {  
  96.                 setImage();  
  97.             }  
  98.             else  
  99.             {  
  100.                 cursor.moveToFirst();  
  101.             }  
  102.             break;  
  103.         case R.id.message:  
  104.             // Dialog显示详细信息  
  105.             AlertDialog.Builder builder = new AlertDialog.Builder(PhotoLibraryActivity.this);  
  106.             builder.setTitle("详细信息");  
  107.             builder.setMessage(Message);  
  108.             builder.setPositiveButton("关闭", new android.content.DialogInterface.OnClickListener() {  
  109.                 public void onClick(DialogInterface dialog, int which)  
  110.                 {  
  111.                     dialog.dismiss();  
  112.                 }  
  113.             });  
  114.             builder.show();  
  115.             break;  
  116.         }  
  117.     }  
  118.   
  119.     private void setImage()  
  120.     {  
  121.         // 获取图片的Name  
  122.         String name = cursor.getString(photoNameIndex);  
  123.         // 获取图片的ID  
  124.         String number = cursor.getString(photoIDIndex);  
  125.         // 获取图片的Title  
  126.         String title = cursor.getString(photoTitleIndex);  
  127.         // 获取图片的大小  
  128.         String size = cursor.getString(photoSizeIndex);  
  129.         // 获取图片存储路径  
  130.         String path = cursor.getString(photoIndex);  
  131.         // 为TextView:position赋值(现在所在的位置)  
  132.         position.setText("(" + number + "/" + totalNum + ")");  
  133.         Message = "Name:" + name + "\n" + "Number:" + number + "\n" + "Title:" + title + "\n" + "Size:" + size + "\n" + "Path:" + path;  
  134.         // 通过路径获取图片  
  135.         Drawable image = Drawable.createFromPath(path);  
  136.         photo.setImageDrawable(image);  
  137.     }  
  138. }  

mian.xml

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/background"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dip"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <Button  
  14.             android:id="@+id/next"  
  15.             android:layout_width="60dip"  
  16.             android:layout_height="50dip"  
  17.             android:background="@drawable/next"  
  18.             android:paddingLeft="30dip" />  
  19.   
  20.         <TextView  
  21.             android:id="@+id/number"  
  22.             android:layout_width="100dip"  
  23.             android:layout_height="50dip"  
  24.             android:paddingLeft="30dip"   
  25.             android:textColor="#000000"/>  
  26.   
  27.         <Button  
  28.             android:id="@+id/previous"  
  29.             android:layout_width="60dip"  
  30.             android:layout_height="50dip"  
  31.             android:background="@drawable/previous"  
  32.             android:paddingLeft="30dip" />  
  33.   
  34.         <Button  
  35.             android:id="@+id/message"  
  36.             android:layout_width="80dip"  
  37.             android:layout_height="40dip"  
  38.             android:paddingLeft="30dip"   
  39.             android:text="详情"/>  
  40.     </LinearLayout>  
  41.   
  42.     <ImageView  
  43.         android:id="@+id/image_view"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="fill_parent" />  
  46.   
  47. </LinearLayout> 

转载于:https://my.oschina.net/Failure/blog/53729

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值