博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何实现带照片缩略图的Listview
阅读量:5103 次
发布时间:2019-06-13

本文共 5422 字,大约阅读时间需要 18 分钟。

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
pictureList = new ArrayList
(); 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 @Override101 public int getCount() {102 // TODO Auto-generated method stub103 return pictureList.size();104 }105 @Override106 public Object getItem(int p) {107 // TODO Auto-generated method stub108 return pictureList.get(p);109 }110 @Override111 public long getItemId(int p) {112 // TODO Auto-generated method stub113 return p;114 }115 @Override116 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 }

 

转载于:https://www.cnblogs.com/dongweiq/p/4112620.html

你可能感兴趣的文章
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
[leetcode]Minimum Path Sum
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Screening technology proved cost effective deal
查看>>
mysql8.0.13下载与安装图文教程
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
Kotlin动态图
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>