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. symfony4 404页面未找到

    symfony4 404页面未找到 安装好symfony4后,发现除了首页能正常显示,其他页面如/_profiler_/等其他创建的router访问都显示The requested URL /xxx ...

  2. Q239 滑动窗口最大值

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: 输入: nums ...

  3. 【Druid】access denied for user ''@'ip'

    今天在写单元测试时,遇到一个很奇葩的问题,一直在报这样的错误: Caused by: java.sql.SQLException: Access denied for user ''@'183.134 ...

  4. @Controller 和 @RestController 区别

    @Controller 用于标识为spring MVC的controller @RestController 是一个便利的注解,加了这个注解就相当于加了@Controller 和 @ResponseB ...

  5. tomcat+nginx+redis集群搭建并解决session共享问题。

    1 集群搭建 https://www.cnblogs.com/yjq520/p/7685941.html 2 session共享 https://blog.csdn.net/tuesdayma/art ...

  6. UVM序列篇之二:sequence和item(上)

    无论是自驾item,穿过sequencer交通站,通往终点driver,还是坐上sequence的大巴,一路沿途观光,最终跟随导游停靠到风景点driver,在介绍如何驾驶item和sequence,遵 ...

  7. <机器学习实战>读书笔记--logistic回归

    1. 利用logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,以此进行分类. 2.sigmoid函数的分类 Sigmoid函数公式定义 3.梯度上升法    基本思想:要找 ...

  8. Firebird 安装多实例

    火鸟数据库的安装向导,默认不允许多实例. 但是不管出于什么原因,若想安装多实例,很简单. 1.先用安装文件,按照向导安装第一个实例. 2.安装后不要启动服务,根据需要配置好Firebird.conf. ...

  9. WeifenLuo.WinFormsUI.Docking.dll的用法

    基本框架 说明 weiFenLuo.winFormsUI.Docking.dll是开源项目DockPanel Suite的一个类库,可以实现像Visual Studio的窗口停靠.拖拽等功能.Weif ...

  10. HTML5--(1)兼容前缀+结构性标签+语义化标签

    一.兼容前缀+兼容写法 兼容前缀 1.HTML5有部分类容兼容到IE9,IE8及以下完全不兼容的内容不再考虑. 2.部分内容需要加兼容前缀 a)     -webkit- 兼容谷歌 b)     -m ...