Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need some direction uploading 2d textures as bytes[]

Discussion in '2D' started by Jay_Kavanagh, Jul 1, 2018.

  1. Jay_Kavanagh

    Jay_Kavanagh

    Joined:
    Aug 20, 2016
    Posts:
    7
    So I am trying to send a selected image from my gallery up to the firebase storage.

    The sample code i am working with has me creating a new byte but my attempt is just creating corrupted data. As an example a 3mb photo is created a 12 mb corrupted file. Obviously wrong but I'm just trying anything at this point.

    The section commented out below my latest attempt is the unity script on the help docs to getRawTexturedata and if i read it correctly it returns as a byte[]. But if thats true how would i correctly reference it in my firebase call further down the page?

    Am I even on the right track?

    Code (CSharp):
    1.     private void PickImageFinished(ePickImageFinishReason _reason, Texture2D _image)
    2.     {
    3.         //current attempt...that does send data but its corrupted
    4.         var custom_bytes = new byte[] {
    5.         };
    6.  
    7.         custom_bytes = _image.GetRawTextureData();
    8.  
    9.         /*code to getRawTextureData
    10.         Texture2D texCopy = new Texture2D(_image.width, _image.height, _image.format, _image.mipmapCount > 1);
    11.         texCopy.LoadRawTextureData(_image.GetRawTextureData());
    12.         texCopy.Apply();
    13.         */
    14.  
    15.         var storageReference = GetStorageReference();
    16.  
    17.         // Create a reference to the file you want to upload
    18.         Firebase.Storage.StorageReference rivers_ref = storageReference.Child("images/rivers.png");
    19.  
    20.         // Upload the file to the path "images/rivers.jpg"
    21.         rivers_ref.PutBytesAsync(custom_bytes)
    22.           .ContinueWith((Task<StorageMetadata> task) => {
    23.               if (task.IsFaulted || task.IsCanceled)
    24.               {
    25.                   Debug.Log(task.Exception.ToString());
    26.           // Uh-oh, an error occurred!
    27.       }
    28.               else
    29.               {
    30.           // Metadata contains file metadata such as size, content-type, and download URL.
    31.          /* Firebase.Storage.StorageMetadata metadata = task.Result;
    32.                   string download_url = metadata.DownloadUrl.ToString();
    33.                   Debug.Log("Finished uploading...");
    34.                   Debug.Log("download url = " + download_url);*/
    35.               }
    36.           });
    37.     }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    You're sending raw texture data, but calling it rivers.jpg? It's not converted to anything else anywhere (unless there's some conversion on the other end?), so I expect the data isn't corrupted, but is whatever _image.format is.

    --Eric
     
  3. Jay_Kavanagh

    Jay_Kavanagh

    Joined:
    Aug 20, 2016
    Posts:
    7
    Got it working with this.

    byte[] custom_bytes = _image.EncodeToPNG();

    var storageReference = GetStorageReference();

    // Create a reference to the file you want to upload
    Firebase.Storage.StorageReference ads_ref = storageReference.Child("images/"+FileName);

    Thanks for the help...
     
    omar_5 and darryl-hill like this.
  4. omar_5

    omar_5

    Joined:
    Feb 8, 2020
    Posts:
    8
    This worked for me!
    I was doing:
    Code (CSharp):
    1. _image.EncodeToPNG();
    2. byte[] custom_bytes = _image.GetRawTextureData();