i'm quite the beginner in Android, i want to develop an activity where the user sees two random Images of his phone gallery, and he has to choose which one's the older one.
So i have this code i found in a tutorial and used it. It works, that it shows every image in the SD Card.
But now is my question:
how do i only get 2 random pics in my gridview?
I hope you can help me, i don't quite get it with this cursor stuff.
public class MainActivity extends Activity {
/**
* Cursor used to access the results from querying for images on the SD
* card.
*/
private Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
private int columnIndex;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which
// columns
// to
// return
null, // Return all rows
null, MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(300, 300));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}
}