【问题标题】:I can't display an image in app from firebase storage using the image url我无法使用图像 url 在应用程序中显示来自 firebase 存储的图像
【发布时间】:2021-06-20 13:46:02
【问题描述】:

我想显示已成功存储到 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# firebase xamarin.forms


【解决方案1】:

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

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, "message": "未找到。无法获取对象", “状态”:“GET_OBJECT” } }

【讨论】:

    【解决方案2】:

    使用 match /{allPaths=**} 更新您的安全规则,以表明所有路径都允许公共读写访问:

    service firebase.storage {
      match /b/zain*******.appspot.com/o {
        match /{allPaths=**} {
          // Allow access by all users
          allow read, write;
        }
      }
    }
    

    【讨论】:

    • 嗨,按照您的建议更改了我的规则,但是我现在收到以下错误代码:响应:{“错误”:{“代码”:400,“消息”:“路径可能不会结束带有 '/' 或包含两个连续的 '/'s" }
    • 您的代码中的 PetProfileImages 是什么?在它之后添加 / 然后试一试...
    • Petprofileimages 是图片上传到的文件夹
    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 2021-08-23
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2019-07-08
    • 2021-06-06
    相关资源
    最近更新 更多