5

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?

4 Answers 4

2

This is how I'm doing it:

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

my embedded images are in the XamarinForms project (PCL)

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Using the ImageLoaderSourceHandler somehow interferes with loading the other images... but github.com/xamarin/mobile-samples/blob/master/EmbeddedResources/… did the trick: var stream = EmbeddedResourceLoader.GetEmbeddedResourceStream("my_graphic"); var bitmap = await BitmapFactory.DecodeStreamAsync(stream);
0

In droid renderer you can set an image to Imageview like this. While using platform specific code use images in platform's file structure. In android Include your images in Drawable folder.

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

1 Comment

I've asked about using embedded resources on Android, not the Android Resource mechanism.
0

This might work for you.

// If your class is inherited from activity or android context then use this.
var imageSource = ImageSource.FromFile("Images/waterfront.jpg");
var img = new ImageLoaderSourceHandler().LoadImageAsync(imageSource,this);

or

// if your class is not from android context then use this.
var imageSource = ImageSource.FromFile("Images/waterfront.jpg");
var img = new ImageLoaderSourceHandler().LoadImageAsync(imageSource,Android.App.Application.Context);

Here is the full implementation

private async Bitmap GetBitMap(path)
{
   var imageSource = ImageSource.FromFile(path);
   return await new ImageLoaderSourceHandler().LoadImageAsync(imageSource,Android.App.Application.Context);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.