Search code examples
androidbitmapxamarinembedded-resourcexamarin.forms

How to load embedded resource into an Android Bitmap with Xamarin?


I'm developing a Xamarin.Forms application which manages some assets in the shared project by using the embedded images feature. Now I have platform specific renderers which also need to access these images.

On iOS I can simply use

UIImage.FromResource(typeof(CustomMap).Assembly, "my_graphic");

to load an embedded image and use it in an UIImageView or similar. How can the same be accomplished in Android specific code?


Solution

  • Not entirely clean and tested but you might want to create an instance of ImageLoaderSourceHandler class and call its LoadImageAsync method passing it an instance of ImageSource, context and CancellationToken (optional). You'll end up with an instance of Android.Graphics.Bitmap.

    Or if you want to bypass Forms you might want to create links to those images in Android project and use them normally. Or load a byte stream from embedded resource