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. [大数据入门] Cloudera-Hadoop 理论

    Hadoop 发明者Doug Cutting Cloudera Hadoop 是基于Java 开发的集群环境,所以每个节点都需要安装Java 运行环境(即JDK),通过Cloudera Manager ...

  2. python并发学习总结

    目录 一.理解操作系统 二.任务类型 三.Socket模块 四.一个简单的C/S程序 五.使用阻塞IO实现并发 方案一:阻塞IO+多进程 方案二:阻塞IO+多线程 阻塞IO模型的思考和总结 六.使用非 ...

  3. 释放linux端口

    感谢作者的共享,在此表示感谢 有时候关闭软件后,后台进程死掉,导致端口被占用.下面以TOMCAT端口8060被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln ...

  4. npm安装过程中的win环境变量设置

    我们要先配置npm的全局模块的存放路径以及cache的路径,例如我希望将以上两个文件夹放在NodeJS的主目录下,便在NodeJs下建立”node_global”及”node_cache”两个文件夹. ...

  5. 【转】C#微信公众平台开发者模式开启代码

    using System; using System.IO; using System.Text; using System.Web.Security; namespace HPZJ.Web.sys. ...

  6. CSS- 文本超出指定宽度后隐藏并显示为省略号

    一般的文字截断(适用于内联与块): .text-overflow { display:block;/*内联对象需加*/ width:25em; word-break:keep-all;/* 不换行 * ...

  7. sublime text3 破解及常用插件

    sublime text3 下载 破解 submelime Text > About sublime Text //看是否注册并 查看当前的版本 然后百度或google搜索 'sublime t ...

  8. 转换json和字符串的一些方法

    将字符串转换成json对象的方法: var str = '{"name1":"value1","name2":"value2&qu ...

  9. Python——爬虫学习1

    爬虫了解一下 网络爬虫(Web crawler),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. Python的安装 本篇教程采用Python3 来写,所以你需要给你的电脑装上Python ...

  10. 禁止选中页面内容-兼容ie、firefox、chrome

    使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart, 比如: 在body中加入<body onselectstart="return fa ...