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. 如何在本地连接服务器上的MySQL

    服务器以阿里云为例 1.首先确保防火墙开放了3306端口 2.确保服务器的linux系统防火墙开放了3306端口 firewall-cmd --list-ports # 查看端口 我这里已经启动了,如 ...

  2. springcloud eureka注册中心 高可复用。

    1:新建两个注册中心项目(名称都为:spring-cloud-eureka,只是端口分别为8000.8001 ).两个注册中心相互注册对方. 2:两个注册中心都启动后,则对方服务列表都有对方的服务. ...

  3. 连接Git@OSC操作步骤

    一.准备工作 软件下载 Git:地址 TortoiseGit:地址 二.安装与配置 1.Git安装 Git配置 设置客户端的用户名和email 然后,到Git@OSC 上面注册一个帐号. 这个帐号就是 ...

  4. (转)[小工具] Linux下列格式化工具 - column

    当你看到Linux下命令输出的列歪歪扭扭时,是不是看着很不爽?column 命令就可以方便地解决这个问题. 比如: 我们一般就只用到上面这一个用法. column的其他用法如下: 选项 含义 -c 字 ...

  5. springboot入门神器 -http://start.spring.io/(在线项目构建)

    参考并直接引用:http://www.sousou.io/article/1506656459859 最近在学习spring boot,看的书是<JavaEE开发的颠覆者 Spring Boot ...

  6. Java集合类中的Iterator和ListIterator的区别

    注意:内容来自网络他人文章! 最近看到集合类,知道凡是实现了Collection接口的集合类,都有一个Iterator方法,用于返回一个实现了Iterator接口的对象,用于遍历集合:(Iterato ...

  7. Spring Security OAuth 2开发者指南译

    Spring Security OAuth 2开发者指南译 介绍 这是用户指南的支持OAuth 2.0.对于OAuth 1.0,一切都是不同的,所以看到它的用户指南. 本用户指南分为两部分,第一部分为 ...

  8. ECharts概念学习系列之ECharts是什么?

    不多说,直接上干货! http://echarts.baidu.com/index.html http://echarts.baidu.com/echarts2/index.html 开源的EChar ...

  9. Javac源码解读-书目录

    1.Javac编译器 (1)Javac编译器介绍(主要介绍如何从java源代码到class的一个转换过程) (2)Javac的源码(说明其中哪个功能由哪个主要的类来完成) (3)Javac支持的命令及 ...

  10. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...