首页 技术 正文
技术 2022年11月10日
0 收藏 922 点赞 4,010 浏览 3549 个字

GridView的XML布局:

main.xml:

<GridViewxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

imageitem.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textid"/>
<ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>

Activity:

 public class FileimageActivity extends Activity {
private static final String TAG = "Fileimage";
private List<HashMap<String, String>> list;
private ContentResolver cr;
private List<ImageView> imageViews = null;
HashMap<String,Object> hashMap;
public Bitmap bitmap;
public Bitmap newBit;
public void onCreate(Bundle savedInstanceState)
  {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findimagepath();
}
public void findimagepath()
GridViewgridView=(GridView)findViewById(R.id.gridview);
list=newArrayList<HashMap<String,String>>();
cr=getContentResolver();
String[]imagedata={Thumbnails._ID,Thumbnails.IMAGE_ID,Thumbnails.DATA};//找到image
Cursorcursor=cr.query(Thumbnails.EXTERNAL_CONTENT_URI, imagedata, null,
null,null); //利用游标找image
String[] from = { "image_id", "path" };
int[] to = { R.id.textid, R.id.ItemImage }; //在imageView.xml的布局中
getColumnData(cursor);
ListAdapteradapter = new picview(this, list, R.layout.imageitem,
from,to);
gridView.setAdapter(adapter);
}//找到image的path,再放进list里。 private void getColumnData(Cursor cur) {
if(cur.moveToFirst())
{
int_id;
intimage_id;
Stringimage_path;
int_idColumn = cur.getColumnIndex(Thumbnails._ID);
intimage_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
intdataColumn = cur.getColumnIndex(Thumbnails.DATA);
Log.i(TAG, String.valueOf(_idColumn)) ;
do{
//Get the field values
_id= cur.getInt(_idColumn);
image_id= cur.getInt(image_idColumn);
image_path= cur.getString(dataColumn); //Do something with the values.
//Log.i(TAG, _id + " image_id:" + image_id + " path:"
//+ image_path + "---");
HashMap<String,String> hash = new HashMap<String, String>();
hash.put("image_id",image_id + "");
hash.put("path",image_path);
list.add(hash); }
while(cur.moveToNext()); } } //很多时候GridView 是利用BaseAdapter来显示图片更多,BaseAdapter的图片资源多少来自drawable的资源。不过其实用SimpleAdapter就已经可以, //重点是SimpleAdapter中有setViewImage这个方法。 class picview extends SimpleAdapter{
     public picview(Context context, List<? extends Map<String, ?>> data,
intresource, String[] from, int[] to) {
super(context,data, resource, from, to);
}
/*
* Called by bindView() to set theimage for an ImageView but only if there is no existing ViewBinder or i
* f the existing ViewBinder cannothandle binding to an ImageView.
* By default, the value will betreated as an image resource.
* If the value cannot be used asan image resource, the value is used as an image Uri.
* This method is called insteadof setViewImage(ImageView, int)
* if the supplied data is not an int orInteger.
* @ value为image的地址 即为path
* Parameters
* v ImageView to receive an image
value the value retrieved from the data set */
public void setViewImage(ImageView v, String value)
{
try
{
FileInputStreamfis = new FileInputStream(value);
BufferedInputStreambis = new BufferedInputStream(fis);
bitmap= BitmapFactory.decodeStream(bis);
newBit= Bitmap.createScaledBitmap(bitmap, , , false);
v.setImageBitmap(newBit); bis.close();
fis.close();
}
catch (Exceptione)
{
Log.e(TAG,e.toString());
v.setImageURI(Uri.parse(value));
}
}
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905