在Android中,对于多媒体文件,系统提供了媒体扫描(MediaScanner)服务,在扫描到相应的文件后,该文件的信息会被存到特定的数据库中。对于图像文件,也是如此。对于SD卡,其URI地址为android. provider.MediaStore.Images. Media. EXTERNAL_CONTENT_URI,对于终端空间,其URI地址为android. provider.MediaStore.Images. Media. INTERNAL_CONTENT_URI。数据库包含的字段包括:DATA、SIZE、DISPLAY_NAME、TITLE、DATE_ADDED、DATE_MODIFIED、MIME_TYPE等。
package com.miaozl.test;
在UI层面,多个图像的浏览通常是通过Gallery来实现的,下面是利用Gallery进行图像浏览的一个实例:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new GalleryAdapter(this));
}
private class GalleryAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
R.drawable.photo1,
R.drawable.photo2,
R.drawable.photo3,
R.drawable.photo4,
R.drawable.photo5,
R.drawable.photo6,
};
public GalleryAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
return i;
}
}
}
下图是图像浏览的效果图。

如果加载的是数据库中的图片,基于的适配器的基类为CursorAdapter。