Search code examples
androidandroid-layoutandroid-fragmentsandroid-recyclerview

How to get all the image from gallery to my application using recyclerview?and how to set image in onbind holder for loding gallery images


  public void onBindViewHolder(MyAdapter.MyViewHolde holder, int position) {
        //how to set image for loading the images for gallery 



----------


}

//and code for how to get images from gallery in main activity


Solution

  • You have to first get all images from gallery in you activity then have to set the list of images to RecyclerView

    Use below method to get all all images-

        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 = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    
            String[] projection = { MediaColumns.DATA,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
    
            cursor = activity.getContentResolver().query(uri, projection, null,
                    null, null);
    
            column_index_data = cursor.getColumnIndexOrThrow(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;
        }
    

    Now set the This list of images to RecyclerView Adapter.

    Take this RecyclerView Example as an reffrence to set all gallery images.