Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- /** The images. */
- public ArrayList<String> images;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- GridView gallery = (GridView) findViewById(R.id.galleryGridView);
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
- if (null != images && !images.isEmpty())
- Toast.makeText(
- getApplicationContext(),
- "position " + position + " " + images.get(position),
- 300).show();
- ;
- }
- });
- }
- }
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- public class ImageAdapter extends BaseAdapter {
- private Activity context;
- public ArrayList<String> images;
- public ImageAdapter(Activity localContext) {
- context = localContext;
- this.images = getAllShownImagesPath(context);
- }
- public int getCount() {
- return images.size();
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(final int position, View convertView, ViewGroup parent) {
- ImageView picturesView;
- if (convertView == null){
- picturesView = new ImageView(context);
- picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
- picturesView.setLayoutParams(new GridView.LayoutParams(325, 325));
- } else {
- picturesView = (ImageView) convertView;
- }
- Glide.with(context).load(images.get(position)).placeholder(R.drawable.ic_launcher).centerCrop().into(picturesView);
- return picturesView;
- }
- private ArrayList<String> getAllShownImagesPath(Activity activity) {
- Uri uri;
- Cursor cursor;
- int column_index_data, column_index_folder_name;
- ArrayList<String> listOfAllImages = new ArrayList<String>();
- String absolutePathOfImage = null;
- uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
- cursor = activity.getContentResolver().query(uri, projection, null, null, null);
- column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
- column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
- while (cursor.moveToNext()){
- absolutePathOfImage = cursor.getString(column_index_data);
- listOfAllImages.add(absolutePathOfImage);
- }
- return listOfAllImages;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement