首页 > 解决方案 > 我无法使用图像 url 在应用程序中显示来自 firebase 存储的图像

问题描述

我想显示已成功存储到 Firebase 存储中的图像。我让用户从图库中选择图像并将其保存在数据库中。然后我将 URL 链接保存在实时数据库中,我认为我可以绑定到 image.source 但这不起作用。我还尝试将链接从 firebase 控制台直接复制到不起作用的 XAML 中。我还尝试将图像下载到手机上(下面的代码),但这不起作用。我在任何地方都找不到任何教程来为 Xamarin 表单显示来自 firebase 的图像。我有点卡在这里。

 public void LoadImages(string imgurl)
     {
         var webClient = new WebClient();
         byte[] imgBytes = webClient.DownloadData(imgurl);
         //string img = Convert.ToBase64String(imgBytes);
         petimage.Source = ImageSource.FromStream(() => new MemoryStream(imgBytes));                     
     }


protected override void OnAppearing()
     {
         base.OnAppearing();
         RetrivePetInfo();                                 
     }

private async void RetrivePetInfo()
     {           
         var pet = await firebaseHelper.GetPet(PetName);
         if (pet != null)
         {
                

             if(pet.imageUrl!=null)
             {
                 LoadImages(pet.imageUrl);                   
             }                                            
         }
     }

标签: c#firebasexamarin.forms

解决方案


我已经看过其他问题了。我尝试了以下代码:

public async Task<ImageSource> LoadImages(string fileName)
    {
        var webClient = new WebClient();
        var stroageImage = await new FirebaseStorage("zain*******.appspot.com")
            .Child("PetProfileImages")
            .Child(fileName)
            .GetDownloadUrlAsync();
        string imgurl = stroageImage;
        byte[] imgBytes = webClient.DownloadData(imgurl);
        //string img = Convert.ToBase64String(imgBytes);
        img = ImageSource.FromStream(() => new MemoryStream(imgBytes));
        return img;
    }

但是我收到一个新的firebase错误:(Firebase.Storage.FirebaseStorageException:'处理请求时发生异常。网址:https ://firebasestorage.googleapis.com/v0/b/*********.appspot .com/o/PetProfileImages%2F 响应:

{

“错误”:{“代码”:404,“消息”:“未找到。无法获取对象”,“状态”:“GET_OBJECT”}}


推荐阅读