Search Unity

Native Gallery for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Feb 28, 2018.

  1. j04milan

    j04milan

    Joined:
    Feb 6, 2019
    Posts:
    44
    Hey, have you been able to tested it. Seems like the new image picker in android 13 comes with a bunch of new features like no runtime permissions and so on that can break the entire plugin if there is no device android version check before picking a file. Hope you endup with a good result. Thanks
     
    yasirkula likes this.
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Not yet, today hopefully :) I'd recommend checking the GitHub page tomorrow or tonight. PS. There was a last minute intervention, so not yet I'm sorry.
     
    Last edited: Dec 4, 2022
  3. Riken_rds

    Riken_rds

    Joined:
    Apr 4, 2017
    Posts:
    49
    I am facing issue in android 13 .
    While using gallery for taking pictures it's redirect to native permission screen event is permission is granted .this happens only in Android 13
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi! I couldn't understand the problem. You said the permission is granted, so what is the issue you're encountering?
     
  5. sachinhello

    sachinhello

    Joined:
    Dec 27, 2018
    Posts:
    35
    any solution for this problem I add sound on button click but it comes after when i close the share popup in app or game.
    to get sound instant not after closing the share popup.
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If launching the share popup 1 frame after the button click (via a coroutine) doesn't help, then you need to either wait for the sound to finish or unfortunately not play the sound at all. Since Unity activity loses focus, it's normal for the sound to be interrupted, as well.
     
  7. thisisajaikumar

    thisisajaikumar

    Joined:
    Apr 22, 2020
    Posts:
    5
    Hi
    I just want to open the gallery and see the saved image.
    pls help!.
     
  8. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,023
    yasirkula likes this.
  9. thisisajaikumar

    thisisajaikumar

    Joined:
    Apr 22, 2020
    Posts:
    5
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @thisisajaikumar Hi! NativeGallery unfortunately doesn't have a function to open the Gallery with the provided image file path. Application.OpenURL might work but I haven't tried it myself.
     
  11. thisisajaikumar

    thisisajaikumar

    Joined:
    Apr 22, 2020
    Posts:
    5
    Hi Actually i tried that way, that's not working.
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Unfortunately I can't help with this issue :(
     
  13. Bullet89

    Bullet89

    Joined:
    Mar 12, 2015
    Posts:
    22
    Hello here! Thanks to the developer for the wonderful plugin! Tell me how can I just open a gallery and a specific album?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi! Unfortunately you can't do this with NativeGallery.
     
    Bullet89 likes this.
  15. Bullet89

    Bullet89

    Joined:
    Mar 12, 2015
    Posts:
    22
    Good! Can I get a list of images in a specific album and display these images inside the application?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you can get the album's file path by some other means, then you can query the files inside it via Directory.GetFiles and display them in your user interface.
     
    Bullet89 likes this.
  17. alabecki

    alabecki

    Joined:
    Aug 20, 2019
    Posts:
    2
    Hello.
    Is it possible to have a callback called when the user has granted or denied write permission? I want to have a change in the apps UI after the use has either granted or denied permission.
    Code (CSharp):
    1. NativeGallery.RequestPermission
    is a synchronous method, so I cannot wait and respond to the user's input on the OS permission popup. Is there some sort of condition I could use in an
    Code (CSharp):
    1. IEnumerator
    to yield return null until that condition is met? (i.e., when the OS permission popup has been dismissed?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    There isn't an async variant of that function. That function halts the program's execution until the user responds to the OS permission popup. So you can call the function and then check the returned value in the next line to determine if the permission was granted.
     
  19. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Is there a way to remove the media after importing? Like:,
    Code (CSharp):
    1. public void DeleteMedia()
    2.     {
    3.         if (File.Exists(mediaFilePath))
    4.         {
    5.             NativeGallery.DeleteMedia(mediaFilePath, MediaType.Image);
    6.             Debug.Log("Media file deleted successfully.");
    7.         }
    8.         else
    9.         {
    10.             Debug.Log("Media file not found.");
    11.         }
    12.     }
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Leuki Unfortunately not. GetImageFromGallery sometimes can't determine the image's original path anyways (natively copies it from a stream to temporaryCachePath), a delete function won't work in those cases.
     
  21. Shreenath1322

    Shreenath1322

    Joined:
    Apr 7, 2016
    Posts:
    7
    Is it possible to open both front and back camera at the same time in unity?
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  23. dev_unity857

    dev_unity857

    Joined:
    Dec 1, 2020
    Posts:
    3
    Hey, awesome job on the package. I do have a bit of a distinct use-case. Long story short, the package supports saving Images to a specific album but doesn't support loading from a specific album. I just wanted to ask if it is possible to achieve this? Currently my whole Gallery is being shown on load but I just need to display images from the album created by the game. Awaiting response, thanks.
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @dev_unity857 That's unfortunately not possible with this plugin.
     
  25. gamedev_co

    gamedev_co

    Joined:
    Apr 16, 2021
    Posts:
    7
    Hello All, and thanks for the lib Yasir, it is a great effort!
    We have a weird crash which is our top one pointing to NGPermissionCallbackAndroid.OnPermissionResult for Android platform.
    Here is the stack trace :

    Code (CSharp):
    1.  
    2. Crashed: Thread: SIGBUS  0x000000002001278f
    3. #00 pc 0xb37aa4 libil2cpp.so (il2cpp::vm::Runtime::ClassInit(Il2CppClass*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    4. #01 pc 0x58e32d6 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    5. #02 pc 0x3a76aea libil2cpp.so (ImplIsValid [ECPoint.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    6. #03 pc 0x373100e libil2cpp.so (CreateRawPoint [SecP256R1Curve.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    7. #04 pc 0x5970e80 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    8. #05 pc 0x24dadd6 libil2cpp.so (_ctor [ECPublicKeyParameters.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    9. #06 pc 0xb223fa libil2cpp.so (GC_gcj_malloc) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    10. #07 pc 0x5970e7f libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    11. #08 pc 0x24dad66 libil2cpp.so (_ctor [ECPublicKeyParameters.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    12. #09 pc 0xc027ea libil2cpp.so (il2cpp::vm::Object::NewAllocSpecific(Il2CppClass*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    13. #10 pc 0x2fb7c5e libil2cpp.so (DecodePublicKey [BcTlsECDomain.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    14. #11 pc 0x2fb78ca libil2cpp.so (ReceivePeerValue [BcTlsECDH.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    15. #12 pc 0x259886a libil2cpp.so (HandleHandshakeMessage [TlsClientProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    16. #13 pc 0x2fa5ec2 libil2cpp.so (get_Hash [TlsHashSink.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    17. #14 pc 0x2597f6a libil2cpp.so (Handle13HandshakeMessage [TlsClientProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    18. #15 pc 0x25b9fee libil2cpp.so (ProcessHandshakeQueue [TlsProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    19. #16 pc 0x59713f9 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    20. #17 pc 0x25b9b56 libil2cpp.so (ProcessRecord [TlsProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    21. #18 pc 0x259039e libil2cpp.so (ReadRecord [RecordStream.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    22. #19 pc 0x25bacca libil2cpp.so (SafeReadRecord [TlsProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    23. #20 pc 0x25b89ba libil2cpp.so (BlockForHandshake [TlsProtocol.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    24. #21 pc 0x58df10e libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    25. #22 pc 0x125087a libil2cpp.so (Connect [TCPConnector.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    26. #23 pc 0x124d91e libil2cpp.so (ThreadFunc [HTTPConnection.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    27. #24 pc 0x2264e libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    28. #25 pc 0xc03be libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    29. #26 pc 0xb81da libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    30. #27 pc 0xb825e libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    31. #28 pc 0x57bb7 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    32. #29 pc 0x477e5 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    33. #30 pc 0xd256aa libil2cpp.so (Execute [Task.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    34. #31 pc 0xf993ae libil2cpp.so (RunInternal [ExecutionContext.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    35. #32 pc 0x596a91f libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    36. #33 pc 0x58e2962 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    37. #34 pc 0xf991b2 libil2cpp.so (Run [ExecutionContext.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    38. #35 pc 0xd25bd6 libil2cpp.so (ExecuteWithThreadLocal [Task.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    39. #36 pc 0x36c6f libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    40. #37 pc 0x58e2962 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    41. #38 pc 0xd25a0e libil2cpp.so (ExecuteEntry [Task.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    42. #39 pc 0xf993ae libil2cpp.so (RunInternal [ExecutionContext.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    43. #40 pc 0x596a91f libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    44. #41 pc 0x58dea3a libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    45. #42 pc 0xb825e libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    46. #43 pc 0xf94b2e libil2cpp.so (Run [ExecutionContext.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    47. #44 pc 0x59805a6 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    48. #45 pc 0x90597e libil2cpp.so (RuntimeInvoker_TrueVoid_t4861ACF8F4594C3437BB48B6E56783494B843915_NodeInfo_tCF2FDCE8AAC04E2350A32E6891BEDF4BA6DC8451(void (*)(), MethodInfo const*, void*, void**, void*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    49. #46 pc 0xb3a3ca libil2cpp.so (il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    50. #47 pc 0xb45342 libil2cpp.so (il2cpp::vm::Class::SetupMethods(Il2CppClass*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    51. #48 pc 0xb3a226 libil2cpp.so (il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    52. #49 pc 0xb6ec36 libil2cpp.so (il2cpp::icalls::mscorlib::System::Threading::Thread::Thread_internal(Il2CppThread*, Il2CppDelegate*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    53. #50 pc 0xb6ec92 libil2cpp.so (il2cpp::icalls::mscorlib::System::Threading::ThreadStart(void*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    54. #51 pc 0xbe273a libil2cpp.so (il2cpp::os::Thread::RunWrapper(void*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    55. #52 pc 0xb6d526 libil2cpp.so (il2cpp::os::ThreadImpl::ThreadStartWrapper(void*)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    56. #53 pc 0xa8147 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    57. #54 pc 0xa8131 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    58. #55 pc 0x61467 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    59. #56 pc 0xa8131 libc.so (BuildId: 68663d64ac44a935374eee374ddf6c9d)
    60. #57 pc 0xb6d4f6 libil2cpp.so (il2cpp::os::ThreadImpl::Run(void (*)(void*), void*, long long)) (BuildId: 4af02353a1699fa8f035091510ecb10d2575d71d)
    61.  
    ECPoint, SecP256R1Curve kind of classes are coming from the Best Http/2 unity asset that I have used for network communication in my game. I couldn't find the relation between NGPermissionCallbackAndroid.OnPermissionResult and the http library because there is no native calls to OnPermissionResult method from there.

    There is no specific Android version, and mostly happening during the app launch which we won't initiate a file selection.

    Do you have an idea about what is happening and how can I solve the issue?

    Thank you so much!
     
    Last edited: Mar 22, 2023
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @gamedev_co I can't make sense of this stacktrace either. OnPermissionResult is called lots of times and it's combined with handshake stuff even though the native code has no networking functions in it. I'd recommend making sure that no NativeGallery function is called at the time of crash. Perhaps someone added a RequestPermission function somewhere without your knowledge. Otherwise I'm as clueless as you are :]
     
  27. Alex-Some

    Alex-Some

    Joined:
    Oct 30, 2013
    Posts:
    15
    @yasirkula I see the same problem sometimes


    Crashed: Thread: SIGSEGV 0x0000000000000034
    #00 pc 0xce95e8 libil2cpp.so (IntHashMapExtensions_GetValueRefByKey_TisBuyMaterialPopupData_t0DD9ECB1B440066F00E6085B229E424C657D5C66_mBCDC2B153CF7BD66E863E8CEFAB39CE56D92D42E_gshared) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #01 pc 0x29abddc libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #02 pc 0x18a9830 libil2cpp.so (BuyMaterialForGold [MaterialsManager.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #03 pc 0x29a6c5c libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #04 pc 0x29dcdb4 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #05 pc 0x2a0cffc libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #06 pc 0x18a9660 libil2cpp.so (OnMaterialBuy [MaterialsManager.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #07 pc 0x29af114 libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #08 pc 0x29a8ebc libil2cpp.so (OnPermissionResult [NGPermissionCallbackAndroid.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)
    #09 pc 0x18aa9cc libil2cpp.so (OnEventRaised [MaterialsManager.cs]) (BuildId: 3d21c8efa4b091a81155d546e58b68cc4efddb55)


    The most strange part is that RequestPermission wasn't called anywhere in the app before this crash. I don't know how this possible. And also on one previous project I have seen the same kind of stack trace with NGPermissionCallbackAndroid that wasn't called anywhere before. How is it possible that it gets in StackTrace?
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I was gonna suggest an infinite loop that calls NativeGallery.RequestPermission but since you mentioned it isn't used anywhere there, I'm unfortunately as clueless as you are. NativeGallery doesn't inject native code to other parts of the application and is invoked only manually.
     
  29. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    hello @yasirkula the first thing: thanks for the great asset.

    now, with what im struggling:
    I want to save the picture on the directory: Application.persistentDataPath.
    reading the thread, I see you says to do this using: File.WriteAllBytes

    my problem is what I dont know how to do the save of the resized picture (resized by specifying the maxSize)
    this is my code so far:


    Code (CSharp):
    1.     private Texture2D imageToSave;
    2.     public string SaveName;
    3.     public string SavePath;
    4.     public int maxSize;
    5.  
    6.     void Start()
    7.     {
    8.         PickImage(maxSize); //the int value is the max size of the image
    9.     }
    10.  
    11.     private void PickImage(int maxSize)
    12.     {
    13.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    14.         {
    15.             Debug.Log("Image path: " + path);
    16.             if (path != null)
    17.             {
    18.                 // Create Texture from selected image
    19.                 imageToSave = NativeGallery.LoadImageAtPath(path, maxSize, false); //false makes the the texture readeable
    20.                 if (imageToSave == null)
    21.                 {
    22.                     Debug.Log("Couldn't load texture from " + path);
    23.                     return;
    24.                 }
    25.  
    26.                 // Assign texture to a temporary quad and destroy it after 5 seconds
    27.                 GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
    28.                 quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
    29.                 quad.transform.forward = Camera.main.transform.forward;
    30.                 quad.transform.localScale = new Vector3(1f, imageToSave.height / (float)imageToSave.width, 1f);
    31.  
    32.                 Material material = quad.GetComponent<Renderer>().material;
    33.                 if (!material.shader.isSupported) // happens when Standard shader is not included in the build
    34.                     material.shader = Shader.Find("Legacy Shaders/Diffuse");
    35.  
    36.                 material.mainTexture = imageToSave;
    37.  
    38.                 //Destroy(quad, 5f);
    39.  
    40.                 // If a procedural texture is not destroyed manually,
    41.                 // it will only be freed after a scene change
    42.                 //Destroy(texture, 5f);
    43.             }
    44.         });
    45.         Debug.Log("Permission result: " + permission);
    46.  
    47.     }
    48.  
    49.     public void SavePicture() //call it from a button to save the picture
    50.     {
    51.         StartCoroutine(PickedPictureSaveV2());
    52.     }
    53.  
    54.     private IEnumerator PickedPictureSaveV2()
    55.     {
    56.         yield return new WaitForEndOfFrame();
    57.         var bytes = imageToSave.EncodeToJPG(); //I think I need to replace this somehow to save the resized picture
    58.         Destroy(imageToSave);
    59.  
    60.         //Writing bytes to a file
    61. #if UNITY_EDITOR
    62.  
    63.         File.WriteAllBytes(Application.streamingAssetsPath + "/claims/" + SaveName + ".jpg", bytes);
    64.  
    65. #endif
    66. #if !UNITY_EDITOR && UNITY_ANDROID || UNITY_IOS
    67.         //Writing bytes to a file
    68.         //File.WriteAllBytes(Application.dataPath + FileName, bytes);
    69.         File.WriteAllBytes(Application.persistentDataPath + "/Claims/" + SaveName + ".png", bytes);
    70.      
    71. #endif
    72.     }
    can you give me some guidance?, thanks
     
    Last edited: Mar 28, 2023
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @elfasito Hi! Are you trying to save a resized screenshot or save a resized image that's picked from Gallery via GetImageFromGallery?
     
  31. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    @yasirkula Hi, the second, Im trying to save a resized image picked from the gallery

    PS: just now I noticed the name of the function for saving, Its not related lol, I changed it right now.
     
    Last edited: Mar 28, 2023
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Code (CSharp):
    1. NativeGallery.GetImageFromGallery((path) =>
    2. {
    3.     if (path != null)
    4.     {
    5.         Texture2D imageToSave = NativeGallery.LoadImageAtPath(path, maxSize, false);
    6.         if (imageToSave != null)
    7.             File.WriteAllBytes(Application.persistentDataPath + "/Claims/" + Path.GetFileNameWithoutExtension(path) + ".png", imageToSave.EncodeToPNG());      
    8.     }
    9. }
     
    elfasito likes this.
  33. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
  34. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    @yasirkula
    Hello, sorry for bothering you again, the previous method works nice.

    Now Im trying to use the method: NativeGallery.LoadImageAtPathAsync()
    but I cant make it work (novice stuff)
    Can you provide a little example of how to use?.
    Im trying to simply replace to:
    Code (CSharp):
    1. Texture2D imageToSave = NativeGallery.LoadImageAtPathAsync(path, maxSize, false);
    but says I cant convert a task to Texture2d
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Texture2D imageToSave = await NativeGallery.LoadImageAtPathAsync(path, maxSize, false);
    and make sure that the method is
    async
    .
     
    elfasito likes this.
  36. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    @yasirkula thanks again, I ended with some a more complex solution, but works:

    Code (CSharp):
    1. public void GetImageFromGallery() //this only works on android/ios or emulators, save to file not works on unity editor
    2.     {
    3.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((fileLocation) =>
    4.         {
    5.             if (string.IsNullOrEmpty(fileLocation))
    6.             {
    7.                 return;
    8.             }
    9.             else
    10.             {
    11.                 internalFileLocation = fileLocation;;
    12.                 StartCoroutine(LoadImageFromGallery());
    13.             }
    14.         });
    15.     }
    16.  
    17.     private IEnumerator LoadImageFromGallery()
    18.     {
    19.         var tempTexture = NativeGallery.LoadImageAtPathAsync(internalFileLocation, 1536, false);
    20.  
    21.         while (!tempTexture.IsCompleted)
    22.         {
    23.             yield return null;
    24.         }
    25.  
    26.         var texture = tempTexture.Result;
    27.         if (texture != null)
    28.         {
    29.             var rect = new Rect(0, 0, texture.width, texture.height);
    30.             var pixelsPerUnit = 100;
    31.             var sprite = Sprite.Create(texture, rect, Vector2.zero, pixelsPerUnit, 1, SpriteMeshType.Tight);
    32.             faceImage.sprite = sprite;
    33.             var bytes = texture.EncodeToJPG();
    34.  
    35. #if UNITY_EDITOR
    36.             File.WriteAllBytes(Application.streamingAssetsPath + "/Claims/" + SaveName + ".jpg", bytes);
    37.             Debug.Log("Picture resized saved on: " + Application.streamingAssetsPath + "/Claims/");
    38. #endif
    39. #if !UNITY_EDITOR && UNITY_ANDROID || UNITY_IOS //if its android or iOS
    40.             File.WriteAllBytes(Application.persistentDataPath + "/Claims/" + SaveName + ".jpg", bytes);
    41.             Debug.Log("Picture resized saved on: " + Application.persistentDataPath + "/Claims/");
    42. #endif
    43.         }
    44.  
    45.         if (texture == null)
    46.         {
    47.             Debug.Log("Cant found a picture to load");
    48.         }
    49.     }
     
    yasirkula likes this.
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Happy to hear that ^^
     
  38. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    @yasirkula Im using the NativeGallery to load an image to a texture from mobiles and then to upload it on Firebase storage. The image loaded successfully on the texture but when i m trying to upload it on Firebase with iphone iOS fails.

    I think the path is strange --> path: /var/mobile/Containers/Data/Application/A1F05F8E-B0C1-4680-A051-B3FAEF0EE7F4/Library/Caches/pickedMedia1.heic

    Important note: The same code working fine on Android and on Unity Editor. Only with iphone iOS can't upload the image.

    Any idea why?
    Thanks in advance.

    Code (CSharp):
    1. imagePath = "";
    2.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    3.         {
    4.             imagePath = path;
    5.  
    6.             Debug.Log("Image path: " + imagePath);
    7.             if (path != null)
    8.             {
    9.                 // Create Texture from selected image
    10.                 Texture2D Imagetexture = NativeGallery.LoadImageAtPath(path, maxSize, true, true);
    11.  
    12.                 GalleryRawImage.color = Color.white;
    13.                 GalleryRawImage.texture = Imagetexture;
    14. }
    15.         });
    16.  
    17.         print("GalleryRawImage.texture:  " + GalleryRawImage.texture);
    18.         Debug.Log("Permission result: " + permission);
    19.  
    20. var newMetadata = new MetadataChange();
    21.        newMetadata.ContentType = "image/jpeg";
    22.  
    23.        //Create a reference to where the file needs to be uploaded
    24.        StorageReference uploadRef = storageReference.Child(firebasePath);
    25.  
    26.  uploadRef.PutBytesAsync(bytes, newMetadata).ContinueWithOnMainThread((task) =>
    27.         {
    28.            if (task.IsFaulted || task.IsCanceled)
    29.            {
    30.                Debug.Log(task.Exception.ToString());
    31.            }
    32.            else
    33.            {
    34.                Debug.Log("File Uploaded Successfully!");
    35.  
    36.                uploadRef.GetDownloadUrlAsync().ContinueWith((task) =>  //Get Firebase URL link
    37.                {
    38.                    if (task.IsFaulted || task.IsCanceled)
    39.                    {
    40.                        Debug.Log(task.Exception.ToString());
    41.                        printOnConsole(task.Exception.ToString());
    42.                        // Uh-oh, an error occurred!
    43.                    }
    44.                    else
    45.                    {
    46.                        firebaseUrl = (task.Result.OriginalString);
    47.                        Debug.Log(task.Result.OriginalString);  //Get Firebase URL link
    48.                    }
    49.  
    50.                });
    51.            }
    52.  
    53.        });
    54.  
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Your web server may not support HEIC format if you're assigning the selected image file's bytes directly to the bytes array. Rather, you can call Imagetexture.EncodeToJPG or EncodeToPNG to get the bytes array in the specified format (this will convert the Texture to a JPG/PNG image file). You need to pass false as the 3rd parameter of LoadImageAtPath for this to work.
     
  40. aolgun98

    aolgun98

    Joined:
    Apr 1, 2021
    Posts:
    1
    @crackbrain94 I'm trying something similar, but the pictures I upload are deteriorating in pixel quality and the color turns orange. Did you have a similar problem
     
  41. noam_baba

    noam_baba

    Joined:
    Mar 14, 2023
    Posts:
    8
    Hi, Im using this asset and for some reason on some android device(It's look like android 12 and below), the file explorer don't work.
    this is my code:
    Code (CSharp):
    1.  
    2. NativeGallery.GetImageFromGallery(path =>
    3.             {
    4.                 if (string.IsNullOrEmpty(path))
    5.                 {
    6.                     log.logMsg(LogType.Log, $"{nameof(InboxPopup)}: AddAttachmentImageFile(), Did Not Fetch File");
    7.                 }
    8.                 else
    9.                 {
    10.                     log.logMsg(LogType.Log, $"{nameof(InboxPopup)}: AddAttachmentImageFile(), Success !, File Path: {path}");
    11.                     FileInfo currentSelectedFileInfo = new FileInfo(path);
    12.                     log.logMsg(LogType.Log, $"File Size: {currentSelectedFileInfo.Length}");
    13.  
    14.                     string currentSelectedFileAttachmentPath = path;
    15.                     _fileAttachmentObject.SetActive(true);
    16.                     _fileAttachmentNameText.text = currentSelectedFileInfo.Name;
    17.                     var fileSizeInMB = currentSelectedFileInfo.Length / 1000000f;
    18.  
    19.                     _fileAttachmentSizeText.text = IsCurrentSelectedFileSizeOk(currentSelectedFileInfo)
    20.                         ? string.Format(FILE_ATTACHMENT_SIZE_TEXT, fileSizeInMB, string.Empty)
    21.                         : string.Format(FILE_ATTACHMENT_SIZE_TEXT, fileSizeInMB, EXCEEDS_MAX_SIZE_TEXT);
    22.                     AddAttachmentFile?.Invoke(currentSelectedFileInfo,currentSelectedFileAttachmentPath);
    23.                 }
    24.             });
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi, can you explain what happens and what goes wrong? Are there any helpful logs?
     
  43. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
  44. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    Is there a way to convert .heic image from iphone to .jpg and upload it on Firebase?
    I tried the following code:

    Code (CSharp):
    1. byte[] heicBytes = File.ReadAllBytes(heicImagePath);
    2.         Texture2D texture = new Texture2D(2, 2);
    3.         ImageConversion.LoadImage(texture, heicBytes);
    4.  
    5.         byte[] jpegBytes = ImageConversion.EncodeToJPG(texture);
    6.  
    7. uploadRef.PutBytesAsync(jpegBytes, newMetadata).ContinueWithOnMainThread((task) =>
    8.         {
    9. }
    Unfortunately instead of the photo uploaded a small red question mark (?) in firebase.

    Any Idea how to solve it?
    Has the Native gallery a conversion function?
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @crackbrain94 Yes you can load the image using NativeGallery.LoadImageAtPath instead of the current solution. Just don't forget to set the markTextureNonReadable parameter to false.
     
  46. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    @yasirkula Awsome! it works.

    What is the second parameter maxsize?
    How much must be max size for keep image HD, FHD, 4K, 8K?
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    maxSize determines the maximum width or height of the returned Texture2D in pixels. Larger textures will be down-scaled proportionally.
     
  48. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    Deat @yasirkula,

    I m not sure if I understand right.
    How much should be maxSize for the following examples.

    HD: (1,280 x 720 pixels)
    Full HD: (1,920 x 1,080 pixels)
    4K: (3,840 x 2,160 pixels)
    8K: (7,680 x 4,320 pixels)
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    1280
    1920
    3840
    7680

    Note that scaling is proportionally so 1280x1024 is also valid for maxSize=1280. It's the maximum pixel size of a single dimension, and nothing else. To give different maxSize values for each dimension, you must call GetImageProperties and calculate the maxSize with those values.
     
  50. kanekoa12

    kanekoa12

    Joined:
    Aug 21, 2013
    Posts:
    3
    @yasirkula @Alex-Some @gamedev_co Hey I am seeing the same SIGSEGV crash at a high rate related to NGPermissionCallbackAndroid.OnPermissionResult being called many times even though we never invoked RequestPermission. Any ideas or solutions?