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

Upload profile photo at server from Mobile Gallery

Discussion in 'Scripting' started by siddharth3322, Jun 20, 2018.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    I want to upload profile photo at my web server as per game requirement. For picking photo from device, I have used this plugin:

    https://assetstore.unity.com/packages/tools/integration/native-gallery-for-android-ios-112630

    I have used this code to upload photo at web server but at present I can able to load photo within Image view component but can't able to upload at web server.

    Code (CSharp):
    1.  IEnumerator PickImage (int maxSize)
    2. {
    3.     Texture2D texture = null;
    4.     NativeGallery.Permission permission = NativeGallery.GetImageFromGallery (( path) => {
    5.         Debug.Log ("Image path: " + path);
    6.         if (path != null) {
    7.             // Create Texture from selected image
    8.             texture = NativeGallery.LoadImageAtPath (path, maxSize);
    9.             if (texture == null) {
    10.                 Debug.Log ("Couldn't load texture from " + path);
    11.                 return;
    12.             }
    13.  
    14.             profileImage.sprite = Sprite.Create (texture, new Rect (0f, 0f, texture.width, texture.height), new Vector2 (0.5f, 0.5f), 100f);
    15.         }
    16.     }, "Select an image", "image/*", maxSize);
    17.  
    18.     if (texture != null) {
    19.  
    20.         // make texture read and write able
    21.         Texture2D readableTexture = DuplicateTexture(texture);
    22.         Debug.Log ("readable texture width: " + readableTexture.width + " height: " + readableTexture.height);
    23.  
    24.         WWWForm form = new WWWForm ();
    25.         Debug.Log ("player id: " + DataCollection.localPlayer.PlayerId);
    26.         form.AddField (GameConstants.ARG_UPLOAD_PHOTO_PLAYER_ID, DataCollection.localPlayer.PlayerId);
    27.         form.AddBinaryData (GameConstants.ARG_UPLOAD_PHOTO, readableTexture.GetRawTextureData (), "profilepic.jpg", "image/*");
    28.  
    29.         Dictionary<string,string> headerDisc = form.headers;
    30.  
    31.         WWW www = new WWW (GameConstants.API_UPLOAD_PHOTO, form.data, headerDisc);
    32.         yield return www;
    33.  
    34.         if (www.error == null) {
    35.  
    36.             Debug.Log ("Data: " + www.text);
    37.             profileImage.sprite = Sprite.Create (texture, new Rect (0f, 0f, texture.width, texture.height), new Vector2 (0.5f, 0.5f), 100f);
    38.  
    39.         } else {
    40.             Debug.Log ("Error: " + www.error);
    41.         }
    42.  
    43.     }
    44.     Debug.Log ("Permission result: " + permission);
    45. }
    Here is the debug output that I was getting:

    dodgels_image_upload_statu.png

    There is some error running that I can't able to understand so please give me some help into this.

    EDIT: Button click event code, at here I have already reduced image size to web server compatible settings:

    Code (CSharp):
    1. public void OnChangePhotoButtonClick ()
    2. {
    3.     // Don't attempt to pick media from Gallery/Photos if
    4.     // another media pick operation is already in progress
    5.     if (NativeGallery.IsMediaPickerBusy ())
    6.         return;
    7.  
    8.     // Pick a PNG image from Gallery/Photos
    9.     // If the selected image's width and/or height is greater than 512px, down-scale the image
    10.     StartCoroutine (PickImage (256));
    11. }
    If I use RestClient or PostMan then in that image get uploaded properly.

    mHEft.png

    Here is the server side code that used for fetching image:

    scv_15294859473735523423619562514693.jpg
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes. Then you would be actually sending an image in jpg format.
     
    siddharth3322 likes this.
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    EncodeToJPG() worked perfectly for me :)
    Thank you for your help....
     
  6. SpyGuy

    SpyGuy

    Joined:
    Apr 12, 2015
    Posts:
    6
    1. // make texture read and write able
    2. Texture2D readableTexture = DuplicateTexture(texture);
      in this you are making texture read and write able can you please share the code for this since this function is not mentioned in Code
      Thanks i am getting an error of texture is not readable i am also picking image from gallery
     
  7. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    Sorry, I can't able to find Duplicate Texture method within my code so you have to find within Internet.
     
  8. Deleted User

    Deleted User

    Guest

    Hi mate can you teach me how you upload the selected image from gallery to server ? basically i am using same native gallery and getting tecture of selected image i am stuck with uploading to firebase ..help
     
  9. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Code (CSharp):
    1.     public void OnClickUploadImageButton()
    2.     {
    3.         NativeGallery.Permission per = NativeGallery.RequestPermission();
    4.  
    5.         // probably should do some permissions checking here
    6.  
    7.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    8.         {
    9.             if (path != null)
    10.             {
    11.                 // Create Texture from selected image
    12.  
    13.                 Texture2D texture = NativeGallery.LoadImageAtPath(path, 2073600, false);
    14.  
    15.                 if (texture == null) return;
    16.  
    17.                 StartCoroutine(UploadImage(texture));
    18.             }
    19.         }, "Select an image", "image/*");
    20.     }
    21.  
    22.     IEnumerator UploadImage(Texture2D theimage)
    23.     {
    24.         var form = new WWWForm();
    25.         form.AddBinaryData("imagedata", theimage.EncodeToPNG(), "uploadedimage");
    26.  
    27.         UnityWebRequestAsyncOperation www = UnityWebRequest.Post("https://www.myrandomserver.com/uploadimage.php", form).SendWebRequest();
    28.  
    29.         yield return www;
    30.  
    31.         if (www.webRequest.isHttpError || www.webRequest.isNetworkError)
    32.         {
    33.             Debug.Log("Error: " + www.webRequest.error);
    34.         }
    35.         else
    36.         {
    37.             Debug.Log("Done!");
    38.         }
    39.     }
    Don't forget to include
    Code (CSharp):
    1. using UnityEngine.Networking;