从Unity到Firebase的简单图像上传不起作用

时间:2019-01-05 23:46:34

标签: c# unity3d firebase-storage

我正在使用我在C#(Unity 2018.2.5f1)中使用Unity创建的IOS应用程序上工作。现在,我尝试将图像上传到Firebase Storage以使其熟悉。不幸的是,这行不通。

我认为本地文件的路径有问题。运行应用程序时,我在XCode中收到以下错误。

Firebase.Storage.Storage.StorageException:所需引用处没有对象。

swift.jpg文件肯定存在于iPhone上。

非常欢迎您的帮助。

using UnityEngine;
using Firebase.Storage;
using System.Threading.Tasks;

public class Upload : MonoBehaviour {

    FirebaseStorage storage;
    StorageReference storage_ref;

    // Use this for initialization
    void Start () {
        storage = FirebaseStorage.DefaultInstance;
        storage_ref = storage.GetReferenceFromUrl("gs://[My Storage URL]");

        // File located on disk
        string local_file = Application.persistentDataPath + "/" + "swift.jpg";

        // Create a reference to the file you want to upload
        StorageReference image_ref = storage_ref.Child("images/swift.jpeg");

        // Upload the file to the path
        image_ref.PutFileAsync(local_file)
          .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 = metadata.DownloadUrl.ToString();
                  // Debug.Log("Finished uploading...");
                  // Debug.Log("download url = " + download_url);
              }
          });
    }

}

1 个答案:

答案 0 :(得分:1)

尝试

string local_file = "file:///" + Application.persistentDataPath + "/" + "swift.jpg";

string local_file = "file://" + Application.persistentDataPath + "/" + "swift.jpg";

string local_file = "file:\\" + Application.persistentDataPath + "/" + "swift.jpg";

希望其中之一会起作用。