package com.wljie.adpater;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import Android.app.Activity;
import Android.content.ContentResolver;
import Android.database.Cursor;
import Android.os.Bundle;
import Android.provider.MediaStore.Images.Thumbnails;
import Android.util.Log;
import Android.widget.GridView;
public class Main extends Activity {
/** Called when the activity is first created. */
private ContentResolver cr;
private final String TAG="Main";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridview);
List> list = getimageList();
gridView.setAdapter(new MyAdpater(this, list));
}
private List> getimageList() {
List> list = new ArrayList>();
cr = getContentResolver();
String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA };
Cursor cur = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection,null, null, null);
if (cur.moveToFirst()) {
int _id;
int image_id;
String image_path;
int _idColumn = cur.getColumnIndex(Thumbnails._ID);
int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
int dataColumn = cur.getColumnIndex(Thumbnails.DATA);
do {
_id = cur.getInt(_idColumn);
image_id = cur.getInt(image_idColumn);
image_path = cur.getString(dataColumn);
Log.i(TAG, _id + " image_id:" + image_id + " path:"+ image_path + "---");
HashMap hash = new HashMap();
hash.put("image_id", image_id +"");
hash.put("path", image_path);
list.add(hash);
} while (cur.moveToNext());
}
return list;
}
}
package com.wljie.adpater;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import
Android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;
import android.widget.GridView;
public class Main extends Activity {
/** Called when the activity is first created. */
private ContentResolver cr;
private final String TAG="Main";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridview);
List> list = getimageList();
gridView.setAdapter(new MyAdpater(this, list));
}
private List> getimageList() {
List> list = new ArrayList>();
cr = getContentResolver();
String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA };
Cursor cur = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection,null, null, null);
if (cur.moveToFirst()) {
int _id;
int image_id;
String image_path;
int _idColumn = cur.getColumnIndex(Thumbnails._ID);
int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
int dataColumn = cur.getColumnIndex(Thumbnails.DATA);
do {
_id = cur.getInt(_idColumn);
image_id = cur.getInt(image_idColumn);
image_path = cur.getString(dataColumn);
Log.i(TAG, _id + " image_id:" + image_id + " path:"+ image_path + "---");
HashMap hash = new HashMap();
hash.put("image_id", image_id +"");
hash.put("path", image_path);
list.add(hash);
} while (cur.moveToNext());
}
return list;
}
}
view plaincopy to clipboardprint?
package com.wljie.adpater;
import java.util.HashMap;
import java.util.List;
import Android.content.Context;
import Android.graphics.Bitmap;
import Android.graphics.BitmapFactory;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.BaseAdapter;
import Android.widget.ImageView;
import Android.widget.TextView;
public class MyAdpater extends BaseAdapter {
private List> list ;
private Context context;
public MyAdpater(Context context,List> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyView tag;
if (convertView == null) {
View v = LayoutInflater.from(context).inflate(R.layout.night_item, null);
tag = new MyView();
tag.imageView = (ImageView) v.findViewById(R.id.itemImage);
// tag.textView = (TextView) v.findViewById(R.id.itemText);
// tag.textView2 = (TextView) v.findViewById(R.id.desc);
v.setTag(tag);
convertView = v;
}else{
tag= (MyView)convertView.getTag();
}
String image_path = list.get(position).get("path");
String text = list.get(position).get("image_id");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
Bitmap newBit = Bitmap.createScaledBitmap(bitmap, 100, 80, true);
tag.imageView.setImageBitmap(newBit);
// tag.textView.setText(text);
// tag.textView2.setText(text);
return convertView;
}
class MyView{
ImageView imageView;
TextView textView;
TextView textView2;
}
}
本篇文章来源于 Linux公社网站() 原文链接:
阅读(3781) | 评论(0) | 转发(0) |