Is it possible to display a device's gallery photos in Xamarin?

I'd like to create a view that display's a device's local photos, but can't find a way to accomplish it. I'm able to display images from the web easy enough, but displaying photos from the local device is proving difficult. Is there a way in Xamarin Forms to show a listing of photos that are located directly on the user's device? I'm able to leverage the CrossMedia plugin to open a chooser and select a photo, but I'd like to instead have a view that displays the user's local photos for a more streamlined experience. I'm targeting Android and iOS to start. Thank you very much for any guidance you can offer!

Best Answers

  • kodekreachorkodekreachor ✭✭
    Accepted Answer

    @AlessandroCaliaro thank you very much, that is just the guidance I needed! Below is the method I was able to put together based on the stack link you provided. Now I just need to figure out the same implementation on iOS :)

        public List<string> ListPhotoFiles()
        {
            List<string> listOfAllImages = new List<string>();
            int column_index_data, column_index_folder_name;
            string PathOfImage = null;
            var uri = MediaStore.Images.Media.ExternalContentUri;
    
            string[] projection = { MediaStore.Images.ImageColumns.Data, MediaStore.Images.ImageColumns.BucketDisplayName };
    
            var cursor = MainActivity.Current.ContentResolver.Query(uri, projection, null, null, null);
    
            column_index_data = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
            column_index_folder_name = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.BucketDisplayName);
    
            while (cursor.MoveToNext())
            {
                PathOfImage = cursor.GetString(column_index_data);
    
                listOfAllImages.Add(PathOfImage);
            }
    
            return listOfAllImages;
        }
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited August 14
    You should explain better which is your problem

    Here you can find sample and docs how to work with images

    https://developer.xamarin.com/samples/xamarin-forms/WorkingWithImages/

    About the gallery you could use https://github.com/AndreiMisiukevich/CardView or https://github.com/daniel-luberda/DLToolkit.Forms.Controls

    I have also created a gallery with the new https://docs.microsoft.com/it-it/xamarin/xamarin-forms/user-interface/collectionview/
  • kodekreachorkodekreachor Member ✭✭

    Thank you for your reply Alessandro! :) More specifically, I'm unsure how to access the local device's gallery photos. Ultimately, I'd like to access the gallery and create a list of file paths which I could then bind to a collection view to show thumbnails for all of the photos within the gallery. I'm not concerned with how to bind them to a collection and display the thumbnails, that part I'm comfortable with. I'm more asking if it's possible to query the local device's gallery and get a list of photos within it.

  • kodekreachorkodekreachor Member ✭✭
    Accepted Answer

    @AlessandroCaliaro thank you very much, that is just the guidance I needed! Below is the method I was able to put together based on the stack link you provided. Now I just need to figure out the same implementation on iOS :)

        public List<string> ListPhotoFiles()
        {
            List<string> listOfAllImages = new List<string>();
            int column_index_data, column_index_folder_name;
            string PathOfImage = null;
            var uri = MediaStore.Images.Media.ExternalContentUri;
    
            string[] projection = { MediaStore.Images.ImageColumns.Data, MediaStore.Images.ImageColumns.BucketDisplayName };
    
            var cursor = MainActivity.Current.ContentResolver.Query(uri, projection, null, null, null);
    
            column_index_data = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
            column_index_folder_name = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.BucketDisplayName);
    
            while (cursor.MoveToNext())
            {
                PathOfImage = cursor.GetString(column_index_data);
    
                listOfAllImages.Add(PathOfImage);
            }
    
            return listOfAllImages;
        }
    
Sign In or Register to comment.