现在的位置: 首页 > 综合 > 正文

检索Android SDCard的图片,并显示到imagebutton

2013年10月16日 ⁄ 综合 ⁄ 共 2899字 ⁄ 字号 评论关闭

根据http://blog.csdn.net/chenjie19891104/article/details/6320664整理所得。


注意:如果你在模拟器已经启动的情况下,push了几张图片到SDCard中[试过:mnt/sdcard/Picture路径下],建议将模拟器关了,再重新启动一下,否则,刚刚添加的图片,是没有办法获取到的。这是因为Android是在系统启动的时候来扫描模拟器上SDCard中多媒体文件的。

代码:

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class PhotoManage extends Activity {
	private static final float DISPLAY_WIDTH = 200;
	private static final float DISPLAY_HEIGHT = 200;
	
	private ImageButton photoView;
	private TextView nameView;
	private Cursor cursor;
	
	private String photoPath;
	private Bitmap currPhoto;
	
	//三个变量 用来保存Media.DATA, Media.TITLE,Media.DISPLAY_NAME的索引号,来获取每列的数据
	private int photoIndex;
	private int nameIndex;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        photoView = (ImageButton)this.findViewById(R.id.image_view);  
        photoView.setOnClickListener(clickListener);  
        nameView = (TextView)this.findViewById(R.id.view_name); 
        
        //指定获取的列  
        String columns[] = new String[]{  
                Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME  
        };  
        cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);  
        photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);  
        nameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);  
          
        Log.v("HERE First:", "First Debug");  
        //显示第一张图片,但是首先要判断一下,Cursor是否有值  
        if(cursor.moveToFirst()){  
            showImage();  
        }  
    }
    
    private View.OnClickListener clickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(cursor.moveToNext()){
				showImage();
			}
		}
	};
	/**
	 * 显示图像
	 */
	private void showImage(){
		photoPath = cursor.getString(photoIndex); //获取图片存储位置
		
		Log.v("Photo Path:", photoPath);
		currPhoto = decodeBitmap(photoPath);
		photoView.setImageBitmap(currPhoto);
		nameView.setText(cursor.getString(nameIndex));
	}
	
	/**
	 * 从path中获取图片信息
	 * @param path
	 * @return
	 */
	private Bitmap decodeBitmap(String path){
        BitmapFactory.Options op = new BitmapFactory.Options();  
        op.inJustDecodeBounds = true;  
        Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息  
        //获取比例大小  
        int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);  
        int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);  
        //如果超出指定大小,则缩小相应的比例  
        if(wRatio > 1 && hRatio > 1){  
            if(wRatio > hRatio){  
                op.inSampleSize = wRatio;  
            }else{  
                op.inSampleSize = hRatio;  
            }  
        }  
        op.inJustDecodeBounds = false;  
        bmp = BitmapFactory.decodeFile(path, op);  
        return bmp;  
	}
}

自己写的一个比较挫的main.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:orientation="vertical" >

    <ImageButton
        android:id="@+id/image_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/view_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

抱歉!评论已关闭.