Show Url in image

DeepakDYDeepakDY INMember ✭✭✭

Hello Developers,

I am using 'Firebase Storage' for store image in server

public async Task<string> StoreImages(Stream imageStream)
        {
            try
            {
                var stroageImage = await new FirebaseStorage("chatmessagedemo-fd41b.appspot.com")
                        .Child("XamarinMonkeys")
                        .Child("image.jpg")
                        .PutAsync(imageStream);
                string imgurl = stroageImage;
                return imgurl;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

this method return image url but this url not show in image source if i hit this url on google then download a image.
'https://firebasestorage.googleapis.com/v0/b/chatmessagedemo-fd41b.appspot.com/o/XamarinMonkeys/image.jpg?alt=media&amp;token=0cbcb321-fd8c-4476-bf57-472b23076b9b'
How can show this image in image source.

Best Answer

Answers

  • DeepakDYDeepakDY INMember ✭✭✭

    @LandLu said:
    Try to download it from this url:

    HttpClient client = new HttpClient();
    var response = await client.GetAsync("https://firebasestorage.googleapis.com/v0/b/chatmessagedemo-fd41b.appspot.com/o/XamarinMonkeys%2Fimage.jpg?alt=media&token=0cbcb321-fd8c-4476-bf57-472b23076b9b%27");
    var imageStrem = await response.Content.ReadAsStreamAsync();
    
    MyImage.Source = ImageSource.FromStream(() => imageStrem);
    

    Moreover, I've corrected your url to https://firebasestorage.googleapis.com/v0/b/chatmessagedemo-fd41b.appspot.com/o/XamarinMonkeys%2Fimage.jpg?alt=media&token=0cbcb321-fd8c-4476-bf57-472b23076b9b%27
    You can also store it in the local storage and retrieve the path:

    HttpClient client = new HttpClient();
    var response = await client.GetAsync("https://firebasestorage.googleapis.com/v0/b/chatmessagedemo-fd41b.appspot.com/o/XamarinMonkeys%2Fimage.jpg?alt=media&token=0cbcb321-fd8c-4476-bf57-472b23076b9b%27");
    var imageBytes = await response.Content.ReadAsByteArrayAsync();
    
    var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyImage.jpg");
    System.IO.File.WriteAllBytes(path, imageBytes);
    
    // Or return path;
    MyImage.Source = ImageSource.FromFile(path);
    

    Hello @LandLu Your solution working well for me

Sign In or Register to comment.