import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
//为view按钮的单击事件绑定监听器
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 清空names、descs、fileNames集合里原有的数据
names.clear();
descs.clear();
fileNames.clear();
//通过ContentResolver查询所有图片信息
Cursor cursor = getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
//获取图片的显示名
String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));
//获取图片的详细描述
String desc = cursor.getString(cursor.getColumnIndex(Media.DESCRIPTION));
//获取图片的保存位置的数据
byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA));
//将图片名添加到names集合中
names.add(name);
//将图片描述添加到descs集合中
descs.add(desc);
//将图片保存路径添加到fileNames集合中
fileNames.add(new String(data , 0 ,data.length-1));
}
//创建一个List集合,List集合的元素是Map
List<Map<String, String>> listItems = new ArrayList<Map<String,String>>();
//将names、descs两个集合对象的数据转换到Map集合中
for(int i = 0 ;i<names.size();i++){
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("name", names.get(i));
listItem.put("desc", descs.get(i));
listItems.addAll(listItems);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(
MainActivity.this, listItems, R.layout.line,
new String[]{"name","desc"}, new int[]{R.id.name,R.id.desc});
//为show ListView组件设置Adapter
show.setAdapter(simpleAdapter);
}
});
show.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 加载view.xml界面布局代表的视图
View viewDialog = getLayoutInflater().inflate(R.layout.view, null);
//获取viewDialog中ID为image的组件
ImageView image = (ImageView) viewDialog.findViewById(R.id.image);
//设置image显示指定图片
image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(position)));
//使用对话框显示用户单击的图片
new AlertDialog.Builder(MainActivity.this).setView(viewDialog)
.setPositiveButton("确定", null).show();
}
});