Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. /** The images. */
  4. public ArrayList<String> images;
  5.  
  6.  
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11.  
  12. GridView gallery = (GridView) findViewById(R.id.galleryGridView);
  13.  
  14. gallery.setAdapter(new ImageAdapter(this));
  15.  
  16. gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  17.  
  18. @Override
  19. public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
  20. if (null != images && !images.isEmpty())
  21. Toast.makeText(
  22. getApplicationContext(),
  23. "position " + position + " " + images.get(position),
  24. 300).show();
  25. ;
  26.  
  27. }
  28. });
  29.  
  30. }
  31.  
  32. }
  33. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  34. public class ImageAdapter extends BaseAdapter {
  35.  
  36. private Activity context;
  37. public ArrayList<String> images;
  38.  
  39.  
  40.  
  41. public ImageAdapter(Activity localContext) {
  42. context = localContext;
  43. this.images = getAllShownImagesPath(context);
  44. }
  45.  
  46. public int getCount() {
  47. return images.size();
  48. }
  49.  
  50. public Object getItem(int position) {
  51. return position;
  52. }
  53.  
  54. public long getItemId(int position) {
  55. return position;
  56. }
  57.  
  58. public View getView(final int position, View convertView, ViewGroup parent) {
  59.  
  60. ImageView picturesView;
  61.  
  62. if (convertView == null){
  63. picturesView = new ImageView(context);
  64. picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  65. picturesView.setLayoutParams(new GridView.LayoutParams(325, 325));
  66. } else {
  67. picturesView = (ImageView) convertView;
  68. }
  69.  
  70. Glide.with(context).load(images.get(position)).placeholder(R.drawable.ic_launcher).centerCrop().into(picturesView);
  71. return picturesView;
  72. }
  73.  
  74.  
  75. private ArrayList<String> getAllShownImagesPath(Activity activity) {
  76.  
  77. Uri uri;
  78. Cursor cursor;
  79. int column_index_data, column_index_folder_name;
  80. ArrayList<String> listOfAllImages = new ArrayList<String>();
  81. String absolutePathOfImage = null;
  82. uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  83.  
  84. String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
  85.  
  86. cursor = activity.getContentResolver().query(uri, projection, null, null, null);
  87.  
  88. column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
  89. column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
  90.  
  91. while (cursor.moveToNext()){
  92. absolutePathOfImage = cursor.getString(column_index_data);
  93.  
  94. listOfAllImages.add(absolutePathOfImage);
  95. }
  96. return listOfAllImages;
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement