Search code examples
xamarin.androidgallery

How do I read the names of all the images in the gallery?


I read these links: https://learn.microsoft.com/en-us/xamarin/android/platform/content-providers/contacts-contentprovider https://stackoverflow.com/questions/60839832/mono-android-read-contact-and-number-in-listview But I want to read the names of all the images in the gallery and to save these in a combobox.


Solution

  • You could access ContentResolver directly to get a cursor against a ContentProvider.

    List<string> imageNames = new List<string>();
    var imagecursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, null, null,null, null);
    //imagecursor.MoveToFirst();
    if (imagecursor == null || imagecursor.Count <= 0) return;
    while (imagecursor.MoveToNext())
        {
           string name = imagecursor.GetString(imagecursor.GetColumnIndex(MediaStore.Images.ImageColumns.Title));
           imageNames.Add(name);
        }
    imagecursor.Close();
    

    Don't forget to ask for READ_EXTERNAL_STORAGE permission.