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));
}
}
} }

Android GridView显示SD卡的图片的更多相关文章

  1. Android_(控件)使用Gallery浏览手机上SD卡中图片

    运行截图: (发现后面两张照片是自己自拍,大写的尴尬对图片进行涂鸦了!!!) 程序结构: <?xml version="1.0" encoding="utf-8&q ...

  2. Android模拟器使用SD卡

    在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一.      准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...

  3. Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等

    此案例主要说的是Android使用GridView加载SD卡下所有目录,文件夹多层遍历,文件图标修改,设置文件对应打开方式等功能. 如图: 代码: public class GridViewFile ...

  4. Android_(控件)使用ListView显示Android系统中SD卡的文件列表

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: 程序结构: <?xml ver ...

  5. Android BaseAdapter ListView (SD卡中文件目录显示出来)

    首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...

  6. Android 读取手机SD卡根目录下某个txt文件的文件内容

    1.先看activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  7. android之读取SD卡状态

    package xidian.dy.com.chujia; import android.os.Build; import android.os.Environment; import android ...

  8. Android中向SD卡读写数据,读SD卡和手机内存

    package com.example.sdoperation; import java.io.BufferedReader; import java.io.File; import java.io. ...

  9. Android开发之SD卡上文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

随机推荐

  1. Python抓取远程文件获取真实文件名

    用urllib下载远程文件并转存到hdfs服务器,在下载时,下载地址中不一定包含文件名,需要从连接信息中获取. 1 file_url = request.form.get('file_url') 2 ...

  2. php 如何截取中文字符串

    在网站应用中时常需要对相应的字符串进行截取.最常用的是使用substr函数对字符串进行截取. 然而,substr和strlen函数只在处理英文字符串时可以正确使用,在截取中文字符时,时常出现乱码.这时 ...

  3. 【Maven学习】maven-assembly-plugin的使用

    转自http://liugang594.iteye.com/blog/2093607 maven-assembly-plugin使用描述(拷自 maven-assembly-plugin 主页) Th ...

  4. 通过System获取java环境变量的路径

    通过System获取java环境变量的路径代码为: import java.io.FileNotFoundException; import java.io.FileOutputStream; imp ...

  5. maven开发项目中遇到的问题 StartChild.call at java.util.concurrent.FutureTask.run

    这个原因是没有配置 pom中没有配置 <scope>provided</scope>  导致冲突 正确的配置文件如下 <project xmlns="http: ...

  6. css3记事

    1.文字超出省略 text-overflow: ellipsis white-space: nowrap; overflow: hidden; text-overflow: ellipsis; *父元 ...

  7. R语言矩阵matrix函数

    矩阵是元素布置成二维矩形布局的R对象. 它们包含相同原子类型的元素.尽管我们可以创建只包含字符或只逻辑值的矩阵,但是它们没有多大用处.我们使用的是在数学计算中含有数字元素矩阵. 使用 matrix() ...

  8. js中json对象数组按对象属性排序

    在实际工作经常会出现这样一个问题:后台返回一个数组中有i个json数据,需要我们根据json中某一项进行数组的排序. 例如返回的数据结构大概是这样: { result:[ {id:,name:'中国银 ...

  9. IOS Runtime属性关联实现表格编辑文本

    要实现在表格里编辑文本, 表格让我想到了CollectionView,文本让我想起TextView, 做之前想了好久怎么样来获得编辑的是哪个TextView,要获取对应的IndexPath啊,想着之前 ...

  10. laravel数据迁移

    创建数据库迁移: php artisan make:migration create_表名_table 回车后就会创建迁移版本   回滚数据库迁移: php artisan migrate 会将数据按 ...