How do I load an embedded resource in Android Bitmap using Xamarin?

I am developing a Xamarin.Forms application that manages some assets in a shared project using inline image functions . I now have platform-specific renderers that also need to access these images.

On iOS, I can just use

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


load an inline image and use it in a UIImageView or similar. How can this be done in android android code?


source to share

3 answers

Not entirely clean and tested, but you can instantiate the ImageLoaderSourceHandler class and call its LoadImageAsync method passing it an ImageSource instance, context and CancellationToken (optional). You will get an instance of Android.Graphics.Bitmap.

Or, if you want to traverse forms, you can link to these images in your Android project and use them normally. Or download a byte stream from an embedded resource



In the droid renderer, you can set the image to Imageview as follows. Platform code uses images in the platform file structure. In android, include your images in the "Rigid" folder.

        global::Android.Widget.ImageView imageview = new global::Android.Widget.ImageView ();
        imageview.SetBackgroundResource (Resource.Drawable.my_graphic);




This is how I do it:

var key = "my_graphic";
using (var imageStream = Assembly.GetAssembly (typeof(YOURNAMESPACE.App)).GetManifestResourceStream (key))
                   var bitmap = await BitmapFactory.DecodeStreamAsync (imageStream);


my inline images are in a XamarinForms project (PCL)



All Articles