onCreate メソッドで、表示する画像のファイルパスを次のように設定する Gallery アクティビティがあります。
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%LC/images%"},
MediaStore.Images.Media._ID + " DESC");
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
ga.setAdapter(new GallAdapter(this,cursor,columnIndex));
public class GallAdapter extends BaseAdapter {
public Cursor cursor;
private int columnIndex;
private Context context;
int imageBackground;
String url;
Uri uri;
int originalImageId;
int imageID;
int columnData;
ViewGroup myp;
ImageView d;
public GallAdapter(Context ctx, Cursor cur, int cIn ) {
context = ctx;
columnIndex = cIn;
cursor = cur;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
myp = parent;
View v;
if(convertView ==null){
v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
}else{
v = convertView;
}
ImageView photo = (ImageView) v.findViewById(R.id.imageView);
ImageView border = (ImageView) v.findViewById(R.id.borderView);
d = (ImageView) v.findViewById(R.id.delView);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
imageID = cursor.getInt(columnIndex);
// obtain the image URI
uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
url = uri.toString();
// Set the content of the image based on the image URI
originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
photo.setImageBitmap(b);
photo.setScaleType(ImageView.ScaleType.FIT_CENTER);
d.setTag(uri);
d.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String path = getRealPathFromURI((Uri) v.getTag());
alertBox("Warning!", "Are you sure you want to delete this photo?", path, v);
}
});
return v;
}
}
問題は、ギャラリーに最新の画像が表示されないことです。何も表示されないこともあれば、もう存在しない古い画像が表示されることもあります