Pedro Paulo
Pedro Paulo

Reputation: 390

Load all images from resource in Xamarin Forms

I'm creating an offline application in Xamarin Forms, I'm currently saving images in specific projects (Android and iOS) and would like to load them all into a ListView. I tried using the following code:

IEnumerable <string> images = Directory.EnumerateFiles("Resources", "*.jpg", SearchOption.AllDirectories); 

But I was unsuccessfully, could anyone help me please?

Thank you.

Upvotes: 0

Views: 1045

Answers (2)

Pedro Paulo
Pedro Paulo

Reputation: 390

I was able to solve my problem with the following code:

public IEnumerable<string> EnumerateImagesFromResource(Func<FieldInfo, bool> filter)
{
    return typeof(Resource.Drawable).GetFields().Where(filter).Select(s => s.Name);
}

Upvotes: 0

Leo Zhu
Leo Zhu

Reputation: 14956

if you want to do like this,i suggest you to save the images into Assets folder,and you could iterate the folder and filter with .png.

for example in Android :

List<System.IO.Stream> streamList = new List<System.IO.Stream>();
AssetManager assetManager = Assets;
string[] images = assetManager.List("");
for (int i = 0; i < images.Length; i++)
   {
     if (images[i].EndsWith(".png"))
       {
          using (StreamReader sr = new StreamReader(assetManager.Open(images[i])))
             {
               streamList.Add(sr.BaseStream);
             }
       }
   }

Assets in android:

enter image description here

you could get the list of image stream by DependencyService or MessagingCenter

Upvotes: 1

Related Questions