Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390530
  • 博文数量: 214
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 1969
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 01:22
文章分类

全部博文(214)

文章存档

2013年(110)

2012年(104)

我的朋友

分类: Android平台

2013-07-29 16:31:35

学习如何检索并显示媒体库中的图片

点击(此处)折叠或打开

  1. package demo.camera;
  2. import android.app.Activity;
  3. import android.database.Cursor;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Bundle;
  7. import android.provider.MediaStore.Images.Media;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.ImageButton;
  11. import android.widget.TextView;
  12. /**
  13.  * 该类完成图片的检索,显示功能
  14.  * @author Administrator
  15.  *
  16.  */
  17. public class PhotoManager extends Activity {
  18.     
  19.     public static final float DISPLAY_WIDTH = 200;
  20.     public static final float DISPLAY_HEIGHT = 200;
  21.     
  22.     //这里采用ImageButton的原因是有Button的作用
  23.     private ImageButton photoView;
  24.     private TextView nameView;
  25.     
  26.     private Cursor cursor;
  27.     
  28.     private String photoPath; //存放某张图片对应的位置信息
  29.     private Bitmap currPhoto;
  30.     
  31.     //这三个变量主要用来保存Media.DATA,Media.TITLE,Media.DISPLAY_NAME的索引号,来获取每列的数据
  32.     private int photoIndex;
  33.     //private int titleIndex;
  34.     private int nameIndex;
  35.     
  36.     public void onCreate(Bundle savedInstanceState){
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.photo_view);
  39.         
  40.         photoView = (ImageButton)this.findViewById(R.id.image_view);
  41.         photoView.setOnClickListener(clickListener);
  42.         nameView = (TextView)this.findViewById(R.id.view_name);
  43.         
  44.         //指定获取的列
  45.         String columns[] = new String[]{
  46.                 Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME
  47.         };
  48.         //cursor = this.managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
  49.         cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
  50.         photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);
  51.         //titleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);
  52.         nameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
  53.         
  54.         Log.v("HERE First:", "First Debug");
  55.         //显示第一张图片,但是首先要判断一下,Cursor是否有值
  56.         if(cursor.moveToFirst()){
  57.             showImage();
  58.         }
  59.     }
  60.     
  61.     private View.OnClickListener clickListener = new View.OnClickListener() {
  62.         
  63.         @Override
  64.         public void onClick(View v) {
  65.             
  66.             if(cursor.moveToNext()){
  67.                 showImage();
  68.             }
  69.         }
  70.     };
  71.     
  72.     /**
  73.      * 显示图像信息
  74.      */
  75.     private void showImage(){
  76.         photoPath = cursor.getString(photoIndex); //这里获取到的就是图片存储的位置信息
  77.         //这里怎样获取图片呢?看decodeBitmap
  78.         Log.v("Photo Path:", photoPath);
  79.         currPhoto = decodeBitmap(photoPath);
  80.         photoView.setImageBitmap(currPhoto);
  81.         nameView.setText(cursor.getString(nameIndex));        
  82.     }
  83.     
  84.     /**
  85.      * 从path中获取图片信息
  86.      * @param path
  87.      * @return
  88.      */
  89.     private Bitmap decodeBitmap(String path){
  90.         BitmapFactory.Options op = new BitmapFactory.Options();
  91.         op.inJustDecodeBounds = true;
  92.         Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
  93.         //获取比例大小
  94.         int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
  95.         int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
  96.         //如果超出指定大小,则缩小相应的比例
  97.         if(wRatio > 1 && hRatio > 1){
  98.             if(wRatio > hRatio){
  99.                 op.inSampleSize = wRatio;
  100.             }else{
  101.                 op.inSampleSize = hRatio;
  102.             }
  103.         }
  104.         op.inJustDecodeBounds = false;
  105.         bmp = BitmapFactory.decodeFile(path, op);
  106.         return bmp;
  107.     }
  108.     
  109. }
转自http://blog.csdn.net/chenjie19891104/article/details/6320664
阅读(454) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~