2
votes

I am working on a Xamarin Application which is part of another project I've been working on, an API

So the API sends profile picture to the Xamarin application as a byte[], but the Xamarin app is not displaying it.

So far I've tried a number of options from the different platforms including here and still it's not working

I've tried converting the image to a base 64 string and then converting it back to a byte[] before displaying it using

imageProfilePic.Source = ImageSource.FromStream(() => new MemoryStream(base64Stream.ToArray()));

Among other options I've tried is creating the memory stream object using the array directly, I even tried returning the image as a base 64 string then converted it to byte array and use to create a new MemoryStream to display the image.

I'm starting to think there's a serious problem with Xamarin.

Is there a way I can return just an image URL from the API, I think it would be the better option,

I am storing the image in the database as byte[] array, using EF Core and MS SQL

2
if it's base64 data, your have to decode it first, not just convert it to a byte[]. You should write the image data to a file after you download it to verify that it is valid image data. And yes, you can just assign the Image's Source to a url. - Jason
I've already tried that and it's not working. May be I should try saving it as a file and use ImageSource.From() and see what happens. - developerharon
Yes, save it as a file and try again, it would be better. - Jack Hua

2 Answers

3
votes

Can you please convert Base64 string to byte array like this:

var byteArray = Convert.FromBase64String(base64Stream);

And then set the image source;

imageProfilePic.Source = ImageSource.FromStream(()=> new MemoryStream(byteArray));
0
votes

I actually found a way to display, I used the URL, Since I had access to the API, I decided to tweak the API so that instead of storing the images to the database, I stored them in the file server and stored their URL in the database and I would return the URL with the API and use it to access the picture in the format

localhost:/images/picture.jpg

Since I'm hosting the API locally, the localhost would be replaced by the IP address

And in the Android app it would be

picture.Source = ImageSource.FromUri(myUri);

And it worked like a charm