简体   繁体   中英

Unable to Upload Image file to Firebase Storage - Unity C#

Hello I'm trying to upload a local Image file to Firebase Storage via Unity 2018.2.10f1, the error I'm getting is

System.AggregateException: Exception of type 'System.AggregateException' was thrown.

I'm having the help of retrieving local image files from the Android Gallery with an Unity Plugin called "NativeGallery" .

Does anyone know what is causing this error?

Here is my Code:

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

I'm pretty sure it's failing because the native gallery asset only works on mobile and to upload files from Android and iOS to firebase you need to add "file://" in front of the image path that gets returned from native gallerty.

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

You may need to take away a slash for IOS platform so the final string is "File://imagePath" versus "File:///imagePath" for android.

This is the last code.Dont forget the change fb link.

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

private void Start()
{

    storage = Firebase.Storage.FirebaseStorage.DefaultInstance;

    // Create a storage reference from our storage service
    storage_ref = storage.GetReferenceFromUrl("My DB link");
}
//Opens the Android Gallery Prompt/Window
public void PickImageFromGallery()
{
    NativeGallery.GetImageFromGallery((path) =>
    {
        if (path != null)
        {
            imagePath = path;

            Debug.Log("Selected Image " + path);
        }
    });
}
//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(("file://" + 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);
              }
          });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM