Home > other >  Unable to Upload Image file to Firebase Storage Unity C#
Unable to Upload Image file to Firebase Storage Unity C#

Time:09-17

I'm trying to upload a Image file to 'Firebase Storage' with Unity 2020.3.6f1. This is the error I'm getting.

Assets\script\FileManager.cs(72,50): error CS1061: 'StorageMetadata' does not contain a definition for 'profile_ref' and no accessible extension method 'profile_ref' accepting a first argument of type 'StorageMetadata' could be found (are you missing a using directive or an assembly reference?)

I used "NativeGallery". Unity Plugin to retrieve local image files from the Gallery.

Does anyone know why this error appears?

Here is my Code :

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

}

}

CodePudding user response:

The problem is in the line: string download_url = metadata.profile_ref.ToString();

The error is self explanatory. Firebase.Storage.StorageMetadata does not contain a definition for 'profile_ref'.

You can find more information about Firebase.Storage.StorageMetadata class here: https://firebase.google.com/docs/reference/unity/class/firebase/storage/storage-metadata.

CodePudding user response:

To get a download URL for a file in Storage, use:

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