在Android中,對(duì)于多媒體文件,系統(tǒng)提供了媒體掃描(MediaScanner)服務(wù),在掃描到相應(yīng)的文件后,該文件的信息會(huì)被存到特定的數(shù)據(jù)庫(kù)中。對(duì)于圖像文件,也是如此。對(duì)于SD卡,其URI地址為android. provider.MediaStore.Images. Media. EXTERNAL_CONTENT_URI,對(duì)于終端空間,其URI地址為android. provider.MediaStore.Images. Media. INTERNAL_CONTENT_URI。數(shù)據(jù)庫(kù)包含的字段包括:DATA、SIZE、DISPLAY_NAME、TITLE、DATE_ADDED、DATE_MODIFIED、MIME_TYPE等。
package com.miaozl.test;
在UI層面,多個(gè)圖像的瀏覽通常是通過(guò)Gallery來(lái)實(shí)現(xiàn)的,下面是利用Gallery進(jìn)行圖像瀏覽的一個(gè)實(shí)例:
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;
}
}
}
下圖是圖像瀏覽的效果圖。

如果加載的是數(shù)據(jù)庫(kù)中的圖片,基于的適配器的基類為CursorAdapter。