无法将图像文件上传到Firebase存储-Unity C#

时间:2018-10-23 20:58:49

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

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

  

System.AggregateException:引发了类型为'System.AggregateException'的异常。

我正在借助名为"NativeGallery"的Unity插件从Android Gallery中检索本地图像文件。

有人知道导致此错误的原因吗?

这是我的代码:

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);
              }
          });
    }
}

1 个答案:

答案 0 :(得分:0)

我非常确定它会失败,因为本机图库资源仅可在移动设备上运行,并且要将文件从Android和iOS上传到Firebase,您需要在从本机返回的图像路径之前添加“ file://” er。

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

您可能需要在IOS平台上加上一个斜杠,以便最终的字符串是“ File:// imagePath”,而对于Android,是“ File:/// imagePath”。