2

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

4
  • 3
    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. Commented Jul 19, 2020 at 19:35
  • 1
    stackoverflow.com/questions/37080258/… Commented Jul 20, 2020 at 1:51
  • 1
    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. Commented Jul 20, 2020 at 19:31
  • Yes, save it as a file and try again, it would be better. Commented Jul 21, 2020 at 3:10

2 Answers 2

3

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)); 
Sign up to request clarification or add additional context in comments.

3 Comments

I have already tried this but it's not working. I've tried all the ways that I can use ImageSource.FromStream(); but they're all not working
Did you check your base64 string? maybe there is a problem with it.
The API that I'm using is one which I created and I'm running locally, I store the photo's in the database as byte[] array, when I display the photos in the web app, they work very well. But when I send the same photos to the Xamarin Android App, I send it as byte[] array, I've tried changing it to base64 string and back to array so I can display it using ImageSource.FromStream but it's not working. I even tried sending it from the API as base64 string but it still it won't work. Let me try redoing it maybe I can see something I missed
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.