如何实现带照片缩略图的Listview
1 ackage com.demo; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.content.Context; 7 import android.database.Cursor; 8 import android.graphics.Bitmap; 9 import android.graphics.BitmapFactory; 10 import android.os.Bundle; 11 import android.provider.MediaStore; 12 import android.provider.MediaStore.Images; 13 import android.view.LayoutInflater; 14 import android.view.View; 15 import android.view.ViewGroup; 16 import android.widget.BaseAdapter; 17 import android.widget.ImageView; 18 import android.widget.ListView; 19 import android.widget.TextView; 20 21 public class PictureActivity extends Activity{ 22 private ListView picturefiles = null; 23 private Cursor cursor; 24 private ArrayList<PictureInfo> pictureList = new ArrayList<PictureActivity.PictureInfo>(); 25 26 private static final String[] STORE_IMAGES = { 27 MediaStore.Images.Media.DATA, 28 MediaStore.Images.Media.MIME_TYPE, 29 MediaStore.Images.Media.DISPLAY_NAME, 30 MediaStore.Images.Media._ID 31 }; 32 33 /** Called when the activity is first created. */ 34 @Override 35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.picture); 38 showimage(); 39 // 单击显示图片 40 //picturefiles.setOnItemClickListener(new ShowItemImageOnClickListener()); 41 } 42 43 public void showimage(){ 44 picturefiles = (ListView)findViewById(R.id.picture_list); 45 cursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STORE_IMAGES, null, null, null); 46 while(cursor.moveToNext()){ 47 PictureInfo info = new PictureInfo(); 48 49 info.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); 50 info.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE)); 51 info.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)); 52 53 //获取当前Picture对应的Id,然后根据该ID获取其Thumb 54 int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)); 55 /*if(info.bitmap!=null&&!info.bitmap.isRecycled()) 56 { 57 info.bitmap.recycle(); 58 }*/ 59 BitmapFactory.Options options=new BitmapFactory.Options(); 60 options.inDither=false; 61 options.inPreferredConfig=Bitmap.Config.RGB_565; 62 info.bitmap=MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, Images.Thumbnails.MICRO_KIND, options); 63 //然后将其加入到pictureList 64 pictureList.add(info); 65 } 66 System.out.println(pictureList.size()); 67 //然后需要设置ListView的Adapter了,使用我们自定义的Adatper 68 PictureAdapter adapter = new PictureAdapter(this); 69 picturefiles=(ListView)findViewById(R.id.picture_list); 70 picturefiles.setAdapter(adapter); 71 } 72 73 class PictureInfo{ 74 String filePath; 75 String mimeType; 76 Bitmap bitmap; 77 String title; 78 } 79 80 class ViewHolder{ 81 ImageView thumbImage; 82 TextView titleText; 83 } 84 85 /** 86 * 定义一个Adapter来显示缩略图和视频title信息 87 * @author Administrator 88 * 89 */ 90 private class PictureAdapter extends BaseAdapter{ 91 92 private Context mContext; 93 private LayoutInflater inflater; 94 95 public PictureAdapter(Context context){ 96 this.mContext = context; 97 this.inflater = LayoutInflater.from(context); 98 this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 99 } 100 @Override 101 public int getCount() { 102 // TODO Auto-generated method stub 103 return pictureList.size(); 104 } 105 @Override 106 public Object getItem(int p) { 107 // TODO Auto-generated method stub 108 return pictureList.get(p); 109 } 110 @Override 111 public long getItemId(int p) { 112 // TODO Auto-generated method stub 113 return p; 114 } 115 @Override 116 public View getView(int position, View convertView, ViewGroup parent) { 117 ViewHolder holder ; 118 if(convertView == null){ 119 holder = new ViewHolder(); 120 convertView = inflater.inflate(R.layout.line, null); 121 holder.thumbImage = (ImageView)convertView.findViewById(R.id.icon); 122 holder.titleText = (TextView)convertView.findViewById(R.id.file_name); 123 convertView.setTag(holder); 124 }else{ 125 holder = (ViewHolder)convertView.getTag(); 126 } 127 128 //显示信息 129 holder.titleText.setText(pictureList.get(position).title); 130 if(pictureList.get(position).bitmap != null){ 131 holder.thumbImage.setImageBitmap(pictureList.get(position).bitmap); 132 } 133 return convertView; 134 } 135 } 136 }