【问题标题】:Unable to Upload Image file to Firebase Storage - Unity C#无法将图像文件上传到 Firebase 存储 - Unity C#
【发布时间】:2019-03-28 04:55:43
【问题描述】:

您好我正在尝试通过 Unity 2018.2.10f1 将本地图像文件上传到 Firebase 存储,我得到的错误是

System.AggregateException:引发了“System.AggregateException”类型的异常。

我正在使用名为"NativeGallery" 的 Unity 插件从 Android 库中检索本地图像文件。

有谁知道是什么导致了这个错误?

这是我的代码:

protected FirebaseStorage storage;
protected StorageReference storage_ref;
private string imagePath;

private void Start(){

    storage = Firebase.Storage.FirebaseStorage.DefaultInstance;

    // Create a storage reference from our storage service
    storage_ref = storage.GetReferenceFromUrl("my Storage URL");
}
//Opens the Android Gallery Prompt/Window
public void PickImageFromGallery(int maxSize = 1024)
{
    NativeGallery.GetImageFromGallery((path) =>
    {
        if (path != null)
        {
            imagePath = path;

            Debug.Log("Selected Image " + path);
        }
    }, maxSize: maxSize);
}
//Uploads Selected Image File
public void Upload()
{

    if(imagePath != null)
    {
        // Create a reference to the file you want to upload
        StorageReference sRef = storage_ref.Child("Work/Photos/image.jpeg");

        // Upload the file to the path "images/rivers.jpg"
        sRef.PutFileAsync(imagePath)
          .ContinueWith((Task<StorageMetadata> task) => {
              if (task.IsFaulted || task.IsCanceled)
              {
                  Debug.Log(task.Exception.ToString());
              // Uh-oh, an error occurred!
          }
              else
              {
              // Metadata contains file metadata such as size, content-type, and download URL.
              StorageMetadata metadata = task.Result;
                  string download_url = sRef.ToString();
                  Debug.Log("Finished uploading...");
                  Debug.Log("download url = " + download_url);
              }
          });
    }
}

【问题讨论】:

    标签: c# firebase unity3d local-storage firebase-storage


    【解决方案1】:

    我很确定它失败了,因为本机图库资产仅适用于移动设备,并且要将文件从 Android 和 iOS 上传到 Firebase,您需要在从本机返回的图像路径前添加“file://”画廊。

     sRef.PutFileAsync(("file://" +imagePath))
              .ContinueWith((Task<StorageMetadata> task) => {
    

    对于 IOS 平台,您可能需要去掉一个斜线,因此最终字符串是“File://imagePath”,而对于 android,则为“File:///imagePath”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2019-11-06
      • 2021-07-03
      • 2020-07-10
      • 2021-07-28
      • 2020-04-30
      • 2021-09-19
      相关资源
      最近更新 更多