【问题标题】:Unable to Upload Image file to Firebase Storage Unity C#无法将图像文件上传到 Firebase Storage Unity C#
【发布时间】:2021-09-15 11:23:54
【问题描述】:

我正在尝试使用 Unity 2020.3.6f1 将图​​像文件上传到“Firebase 存储”。 这是我遇到的错误。

Assets\script\FileManager.cs(72,50):错误 CS1061:“StorageMetadata”不包含“profile_ref”的定义,并且没有可访问的扩展方法“profile_ref”接受“StorageMetadata”类型的第一个参数找到(您是否缺少 using 指令或程序集引用?)

我使用"NativeGallery". Unity Plugin 从图库中检索本地图像文件。

有人知道为什么会出现这个错误吗?

这是我的代码:

public Image profileImage;


public void BringImage()
{
    PickImage(1024);
}

private void PickImage(int maxSize)
{
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
        Debug.Log("Image path: " + path);
        if (path != null)
        {
            // Create Texture from selected image
            Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
            if (texture == null)
            {
                Debug.Log("Couldn't load texture from " + path);
                return;
            }
            Rect rect = new Rect(0, 0, texture.width, texture.height);
            profileImage.sprite = Sprite.Create(texture, rect, new Vector2(0.5f, 0.5f));

            UploadImage(path);
        }
    });

    Debug.Log("Permission result: " + permission);
}


public void UploadImage(string path)
{

    FirebaseStorage storage = FirebaseStorage.DefaultInstance;
    StorageReference storageRef = storage.GetReferenceFromUrl("gs://modoodle-backend-91a60.appspot.com/");


    Firebase.Storage.StorageReference profile_ref = storageRef.Child(IDRegisterManager.email + "/profileImages/profileImage.jpg");

    // Upload the file to the path IDRegisterManager.email + "/profileImage"
    profile_ref.PutFileAsync("file://" + path)
      .ContinueWith((Task<StorageMetadata> task) =>
      {
          if (task.IsFaulted || task.IsCanceled)
          {
              Debug.Log(task.Exception.ToString());
             
          }
          else
          {
              // Metadata contains file metadata such as size, content-type, and download URL.
              Firebase.Storage.StorageMetadata metadata = task.Result;
              string download_url = metadata.profile_ref.ToString();
              Debug.Log("Finished uploading...");
              Debug.Log("download url = " + download_url);
          }
      });

}

}

【问题讨论】:

    标签: c# firebase unity3d firebase-storage


    【解决方案1】:

    问题出在一行:string download_url = metadata.profile_ref.ToString();

    错误是不言自明的。 Firebase.Storage.StorageMetadata 不包含“profile_ref”的定义。

    您可以在此处找到有关 Firebase.Storage.StorageMetadata 类的更多信息:https://firebase.google.com/docs/reference/unity/class/firebase/storage/storage-metadata

    【讨论】:

    • 所以我将这一行改为'string download_url = metadata.profile_ref.ToString();' ,但它仍然会出现这样的错误:'Assets\script\FileManager.cs(71,48): error CS1061: 'StorageMetadata' does not contain a definition for 'DownloadUrl' and no access extension method 'DownloadUrl'接受第一个参数可以找到“StorageMetadata”类型的(您是否缺少 using 指令或程序集引用?)'。
    • 老实说,我不明白什么是 StorageMetadata 以及如何处理它。我真正想做的就是将图像上传到我从图库中挑选的 Firebase 存储。所以我从 .ContinueWith((Task task) ... 中删除了代码,但仍然没有图像上传到存储...
    • 我建议您阅读 Firebase 文档和教程。这是非常有据可查的。对不起,我没有时间挖北斗。 Frank van Puffelen 先生是这里的专家。
    • 感谢您的帮助!我会寻找解决方案。
    【解决方案2】:

    对于存储中的文件,get a download URL,使用:

    profile_ref.GetDownloadUrlAsync().ContinueWithOnMainThread(task => {
        if (!task.IsFaulted && !task.IsCanceled) {
            Debug.Log("Download URL: " + task.Result);
            // ... now download the file via WWW or UnityWebRequest.
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 2014-06-21
      • 2021-08-22
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多