Android 图片浏览器源码,是你学习Android很好的例子,希望对你的学习有帮助。
main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/grid_background"
/>
Android开发:图片浏览器源码下载地址:
用户名与密码都是www.linuxidc.com
具体下载目录在 /pub/Android源码集锦/2011年/9月/Android开发:图片浏览器源码/
示例:
package com.xmobileapp.pictureviewer;
import java.io.File;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
class PictureShowUtils {
// 图片存储位置,SD卡的pic目录下。
private String dirName = Environment.getExternalStorageDirectory().toString() + "/pic/";
// 用于记录目录下图片名称的字符串数组
String[] filenames = null;
// 在构造函数中初始化filenames
PictureShowUtils () {
try{
filenames = new File(dirName).list();
}
catch(Exception e){
filenames = null;
}
}
// 获取图片数量
public int getCount () {
if(filenames == null)
return 0;
return filenames.length;
}
// 获取指定索引的图片
public Bitmap getImageAt (int i) {
String path = dirName;
if(i>=filenames.length)
return null;
path += filenames[i];
// 使用BitmapFactory.decodeFile读取图片内容
Bitmap b = BitmapFactory.decodeFile(path);
return b;
}
}