博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
遍历指定目录下的图片文件 显示在gridview
阅读量:5020 次
发布时间:2019-06-12

本文共 5126 字,大约阅读时间需要 17 分钟。

gridview布局

item配件布局

主Activity

package com.su.testgrid3;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.GridView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class TestGrid3Activity extends Activity {	private GridView gridView;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);				String[] titles = FileUtil.getImageNames("/mnt/sdcard/k/");		String[] imagePaths = new String[titles.length];		for (int i = 0; i < titles.length; i++) {			imagePaths[i]="/mnt/sdcard/k/"+titles[i];		}				gridView = (GridView) findViewById(R.id.gridview);		PictureAdapter adapter = new PictureAdapter(titles, imagePaths, this);		gridView.setAdapter(adapter);		gridView.setOnItemClickListener(new OnItemClickListener() {			public void onItemClick(AdapterView
parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你点击了" + (position + 1), Toast.LENGTH_SHORT).show(); } }); gridView.setOnItemLongClickListener(new OnItemLongClickListener(){ public boolean onItemLongClick(AdapterView
parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你长按了" + (position + 1), Toast.LENGTH_SHORT).show(); return true; /* * 这里需要true * 因为:OnItemLongClick事件中:down事件返回值标记此次事 * 件是否为点击事件(返回false,是点击事件;返回true,不记为点击事件), * 而up事件标记此次事件结束时间,也就是判断是否为长按。 */ } }); }}
适配器

// 2012-4-18下午07:46:57package com.su.testgrid3;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.graphics.BitmapFactory;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;//自定义适配器class PictureAdapter extends BaseAdapter {	private LayoutInflater inflater;	private List
pictures; public PictureAdapter(String[] titles, String[] images, Context context) { super(); pictures = new ArrayList
(); inflater = LayoutInflater.from(context); for (int i = 0; i < images.length; i++) { Picture picture = new Picture(titles[i], images[i]); pictures.add(picture); } } @Override public int getCount() { if (null != pictures) { return pictures.size(); } else { return 0; } } @Override public Object getItem(int position) { return pictures.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.picture_item, null); viewHolder = new ViewHolder(); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.title.setText(pictures.get(position).getTitle()); viewHolder.image.setImageBitmap(BitmapFactory.decodeFile(pictures.get( position).getImageId())); return convertView; }}class ViewHolder { public TextView title; public ImageView image;}

picture的javabean

 

// 2012-4-18下午07:47:44package com.su.testgrid3;class Picture {	private String title;	private String imageId;	public Picture() {		super();	}	public Picture(String title, String imageId) {		super();		this.title = title;		this.imageId = imageId;	}	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}	public String getImageId() {		return imageId;	}	public void setImageId(String imageId) {		this.imageId = imageId;	}}

获取图片文件的名字数组的操作类

// 2012-4-18下午07:49:44 package com.su.testgrid3;import java.io.File;public class FileUtil {	public static String[] getImageNames(String folderPath) {		File file01 = new File(folderPath);		String[] files01 = file01.list();		int imageFileNums = 0;		for (int i = 0; i < files01.length; i++) {			File file02 = new File(folderPath + "/" + files01[i]);			if (!file02.isDirectory()) {				if (isImageFile(file02.getName())) {					imageFileNums++;				}			}		}		String[] files02 = new String[imageFileNums];		int j = 0;		for (int i = 0; i < files01.length; i++) {			File file02 = new File(folderPath + "/" + files01[i]);			if (!file02.isDirectory()) {				if (isImageFile(file02.getName())) {					files02[j] = file02.getName();					j++;				}			}		}		return files02;	}	private static boolean isImageFile(String fileName) {		String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,				fileName.length());		if (fileEnd.equalsIgnoreCase("jpg")) {			return true;		} else if (fileEnd.equalsIgnoreCase("png")) {			return true;		} else if (fileEnd.equalsIgnoreCase("bmp")) {			return true;		} else {			return false;		}	}}

 

 

 

转载于:https://www.cnblogs.com/sfshine/archive/2012/06/23/2742900.html

你可能感兴趣的文章
2017-12-08 违法数据筛选.sql
查看>>
[Luogu 1640] SCOI2010 连续攻击游戏
查看>>
Ubuntu下的一款Dock工具-AWN
查看>>
【Openwrt】刷
查看>>
【特征值】特征值的几何含义
查看>>
配置魔药
查看>>
Java 多态详解
查看>>
收藏别人的小类库
查看>>
原型设计工具
查看>>
Oracle11gr2_ADG管理之switchover实战
查看>>
使用Spring 线程池
查看>>
Linux学习笔记:rm删除文件和文件夹
查看>>
理解管理信息系统
查看>>
JZOJ5146:港湾
查看>>
列表类型内置方法
查看>>
Object、Function、String、Array原生对象扩展方法
查看>>
了解一下 Linux 上用于的 SSH 图形界面工具
查看>>
设计模式之组合模式
查看>>
insertRule()与addRule()创建规则
查看>>
FMDB中 databaseWithPath 的使用问题
查看>>