Home > Mobile >  Insert Image to firebase using xamarin form
Insert Image to firebase using xamarin form

Time:12-08

My problem is how to insert image in firebase.

I tried some reasearch but it says, use firebase storage, is it possible to have another way like to insert in firebase realtime ? not in storage.

For now this is my codes; registration page.xml `

 <StackLayout
                                Padding="10">
                                <Image 
                                    x:Name="Imgresult" 
                                    HeightRequest="100" />
                                <Button 
                                    Text="Upload Valid ID" 
                                    FontSize="18"
                                    HeightRequest="45"
                                    TextColor="White" 
                                    HorizontalOptions="CenterAndExpand" 
                                    BackgroundColor="#0e3663" 
                                    Clicked="ImageButton_Clicked"
                                    Margin="0,25,0,0"/>

                            </StackLayout>

`

codebehind.cs `

 async void ImageButton_Clicked(object sender, EventArgs e)
        {
            var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Upload a clear copy of your valid ID"
            });

            var stream = await result.OpenReadAsync();

            
        }

` any link that will posted about my post will be appreciated, thank you so much.

CodePudding user response:

The Firebase Realtime Database can only store JSON data types. So to store an image in the database, you'll have to store it as a base64 encoded string.

CodePudding user response:

As @Frank van Puffelen said, you have to convert the image stream to base64 encoded string when using Firebase Realtime db, you could try the following code:

byte[] bytes;
string base64str;
async void ImageButton_Clicked(object sender, EventArgs e)
{
    ...
    var stream = await result.OpenReadAsync();
    var mstream = new MemoryStream();
    stream.CopyTo(mstream);         
    bytes = mstream.ToArray();
    base64str = Convert.ToBase64String(bytes);
}

To insert data into Firebase Realtime database, you could refer to this video: Getting Started with Firebase Realtime Database and Xamarin.Forms and its code sample XFFirebaseRealtimeDBSample.

Hope it works for you.

  • Related