Search Unity

Native Gallery for Android & iOS [Open Source]

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

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    "Select Photos doesn't support synchronously selecting photos": NativeGallery asks permissions synchronously by using a semaphore to lock the main thread but the Select Photos option causes a deadlock with this approach. It is impossible to ask permissions synchronously on iOS 14+.

    NativeGallery.Permission permission = NativeGallery.RequestPermission(NativeGallery.PermissionType.Read);
    is a synchronous method; when you attempt to show permission dialogs on iOS 14+, this function will cause a deadlock and it is impossible to avoid it. To show permission dialogs on iOS 14+, you'd have to create an asynchronous permission function like
    NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Read, permissionCallback);
    . To do that, you'd have to duplicate all native permission functions, strip them of semaphores, pass the permission result to Unity via UnitySendMessage and read this message from a C# script (which must be instantiated by RequestPermissionAsync and it must invoke the callback passed to RequestPermissionAsync). This is a lot of work and it isn't the intended behaviour of NativeGallery, so I unfortunately won't help with this implementation.

    The parts you can study to try implementing this on your own:
     
  2. OrenD

    OrenD

    Joined:
    Jul 14, 2020
    Posts:
    8
    Thank you for the info! I'm a programming beginner so "Semaphore" is new to me and I don't really understand threads yet. My first languages are more high level. I'm guessing semaphore based on wiki is some kind of access variable.
    Will that function cause a deadlock because it's attempting to do something synchronously, but the main thread is already being used? Also why do you lock the main thread? And why not make this multithreaded?

    Also you keep mentioning iOS 14+, so are you saying that iOS < 14 can do this behavior? And is this suggestive that Apple is moving away from requesting permission to view photos in the gallery?
     
    Last edited: Jun 9, 2021
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Semaphore allows freezing a thread until it is explicitly unfrozen by code. I use a semaphore to lock the main thread because it's the only way to ask permissions synchronously. When Select Photos is clicked, it wants to use the main thread for some reason but since the main thread is frozen by the semaphore, it causes a deadlock.

    Select Photos permission is added on iOS 14+. Apple has indeed been moving away from requesting permissions to view photos in gallery since iOS 11+. There is no reason to ask a permission on these iOS versions. If you want to, you have to make your own async permission function as I've said.
     
  4. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, the texture, that I get from the asset, will change after a few seconds of creating a sprite.
    This is the image the moment it's added:
    1.PNG
    Here is the image a few seconds after:
    1.1.PNG

    If I set the wrapMode and filterMode.
    texture.wrapMode = TextureWrapMode.Clamp;
    texture.filterMode = FilterMode.Bilinear;

    Then the texture will change to this:
    1.2.PNG

    Here is the code for getting the texture:
    texture = NativeGallery.LoadImageAtPath(path, -1, false, false);

    Here is how I create the sprite:
    Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));

    Any ideas? Thanks.
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    I've never seen this before. If you can reproduce the issue when you use Texture2D.LoadImage instead of NativeGallery.LoadImageAtPath, then the issue must be caused by something else, not NativeGallery.
     
  6. khizerk77

    khizerk77

    Joined:
    Jun 18, 2021
    Posts:
    2
    Hey @yasirkula I need to support multiple images selection on older versions of Android and iOS! Any pointers on what I need to modify with your plugin to achieve this?

    Also I am wondering why it doesn't support older versions, Any permissions related issues for old OS versions.
     
    Last edited: Jun 18, 2021
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    If it were possible, I'd do it. Old OS versions don't have this native functionality. You'd have to create your own native gallery app from scratch using Android Studio.
     
    khizerk77 likes this.
  8. khizerk77

    khizerk77

    Joined:
    Jun 18, 2021
    Posts:
    2
    What about iOS 13. I see you are using "UIImagePickerController" for older versions. And there are other 3rd party options for iOS native that provides multiple image selection.
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Yes there are 3rd party solutions on GitHub, though they don't have Unity wrappers. I'm not planning to use 3rd party solutions in my plugin but if you want, you can download those 3rd party solutions from GitHub, create Unity wrappers for them and see if everything goes smoothly. Then you can use NativeGallery on iOS 14+ and the 3rd party solution on older iOS versions.
     
    khizerk77 likes this.
  10. lucas_martinic

    lucas_martinic

    Joined:
    Jun 13, 2018
    Posts:
    5
    Thanks for the plugin. I am using it for getting permisisons on iOS and the write permission gets prompted to the user, but I'm not getting any prompt for the read permission. What am I missing? I am testing in iOS 14.6
    edit: if I check the permission status, I get the enum value "allowed".
     
    Last edited: Jun 23, 2021
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You aren't missing anything:
    If you absolutely have to ask the permission on these iOS versions, then you can't use NativeGallery because it has a known permission dialog issue on iOS 14+ (which doesn't normally affect the plugin because the permission dialog isn't needed on iOS 11+, as quoted above).
     
  12. JacekCi

    JacekCi

    Joined:
    Feb 23, 2018
    Posts:
    6
    After updating to XCode 12.5 and Unity to 2020.3.12f1 we are getting this error in XCode:

    Code (CSharp):
    1. /{our project...}/Libraries/Plugins/NativeShare/iOS/NativeShare.mm:28:48: Cannot initialize a parameter of type 'id<MFMailComposeViewControllerDelegate> _Nullable' with an lvalue of type 'const Class'
    Once I removed that code I got another issue:
    Code (CSharp):
    1. /Libraries/Plugins/iOS/MediaFileHelper.mm:38:23: Incompatible pointer types assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from 'Class'
    Please advise.
     

    Attached Files:

    Last edited: Jun 24, 2021
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    The screenshot you've posted isn't a part of my plugins, you must check out your other plugins for that. The second error message says MediaFileHelper.mm and not NativeGallery.mm, so I'd say that it isn't caused by NativeGallery either but NativeGallery had a similar issue long ago and updating the plugin to the latest version resolves it: https://github.com/yasirkula/UnityNativeGallery/issues/200
     
    JacekCi likes this.
  14. JacekCi

    JacekCi

    Joined:
    Feb 23, 2018
    Posts:
    6
    Thanks! I thought we are on latest. That link helped me.
     
    yasirkula likes this.
  15. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hey, is there any way i can save an image from a byte[] to a desired location? When i am using the SaveImageToGallery function it is actually saving it somewhere else. I need it to save in a specific location.
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    What is this specific location? If it is something like /storage/emulated/0/CustomFolder, you can't do it with NativeGallery.
     
  17. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Oh, lol, the location is Application.PersistantDataPath + some folder.
    Do you know how to make that happen ?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    In that case, you can call
    File.WriteAllBytes(path, byteArray);
     
  19. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Thanks
     
  20. ashlit1998

    ashlit1998

    Joined:
    Jan 25, 2019
    Posts:
    10
    Hello!
    The asset works perfectly on Android, but iOS is causing us a few problems.
    I was checking the forum to see if anyone had gotten a failure in Unity cloud builds on iOS. I'm using the latest version of 2019.4.x to build and the latest Xcode version. Unfortunately, I seem to be getting this error:

    11046: ▸ Linking UnityFramework
    11047: ▸ ❌; Undefined symbols for architecture arm64
    11048: ▸ > Symbol: _saveToGallery
    11049: ▸ > Referenced from: _ScreenshotHandler_saveToGallery_mAF92A9D3A95573F111FD365EA31BC56FAEB24F1F in Assembly-CSharp8.o
    11050: ▸ ❌; ld: symbol(s) not found for architecture arm64
    11051: ▸ ❌; clang: error: linker command failed with exit code 1 (use -v to see invocation)
    11052: ▸ Linking UnityFramework
    11053: ▸ ❌; Undefined symbols for architecture armv7
    11054: ▸ > Symbol: _saveToGallery
    11055: ▸ > Referenced from: _ScreenshotHandler_saveToGallery_mAF92A9D3A95573F111FD365EA31BC56FAEB24F1F in Assembly-CSharp8.o
    11056: ▸ ** ARCHIVE FAILED **
    11057: ▸ The following build commands failed:
    11058: ▸ Ld /BUILD_PATH/Library/Developer/Xcode/DerivedData/Unity-iPhone-gpapcbdyymujeqbzzienkgtqokhi/Build/Intermediates.noindex/ArchiveIntermediates/Unity-iPhone/IntermediateBuildFilesPath/Unity-iPhone.build/Release-iphoneos/UnityFramework.build/Objects-normal/arm64/UnityFramework normal arm64


    Apparently someone asked this question a while back and said they were using unity 2019.4.5, with the suggested solution being "remove and reinstall native gallery", so I deleted it and readded it to the project via the asset store, only to get the same error again. When I removed Native Gallery, the build succeeded normally and it all went well. Any idea what I can do to set it to work?

    Thank you!!
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    NativeGallery doesn't have a _ScreenshotHandler_saveToGallery function, all of its function start with _NativeGallery. Another plugin must be causing this issue.
     
  22. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    96
    Hi I have a recent version of the plug-in but when I save a photo on iOS 14.6 it won’t send the picture to the gallery I set. It just appears in the normal Photos folder.

    I tested on another lower os and it did save it to the folder.

    Do you know the reason for this?

    thanks for any help
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Quoting from the documentation:
    Saving to custom albums require full Photos permission on iOS 14+ which can't be obtained via NativeGallery.
     
    GustavNinja likes this.
  24. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    96
    Oh Okay. Thanks for the reply.
    Will it be available in the future to be able to set "All Photos" permission in the plugin?
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    NativeGallery has a known issue with the permission dialog that asks for that permission, so I have no plans at the moment.
     
  26. XStudios

    XStudios

    Joined:
    Oct 27, 2014
    Posts:
    2
    Hi
    I've been using this awesome asset for years for both ios and android without any issues.
    Lately we had to update our app that uses this asset. I built successfully for android without any issues, but when I build for ios and try to push the build to my phone I get this errors in xcode in 2 places (screenshots attached):

    Code (CSharp):
    1. Incompatible pointer types assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from 'Class'
    more info:
    I'm using Unity 2019.3.12 on mac.
    I'm using the latest xcode (version 12.5.1)

    I didn't find the settings : Project Settings/yasirkula/Native Gallery in unity to enable automated setup, so I assumed it's by default set to automatic.

    I read in the thread that someone had the same issue and they said there's a patch to apply, but I didn't find that either. :(
    Please let me know how I can resolve it.

    Thanks a lot.
     

    Attached Files:

    Last edited: Jul 12, 2021
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  28. XStudios

    XStudios

    Joined:
    Oct 27, 2014
    Posts:
    2
    @yasirkula Thanks a lot for the fast response. That fixed it :)
    Interestingly, I tried updating the asset before posting here but I guess was solved it was to delete the asset from the app data folder, and from the plugins folder, restart unity and reimport it again.
     
    yasirkula likes this.
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Yes, this is unfortunately a Unity Asset Store bug that many users are unaware of. I'm surprised that Unity team have released many Unity versions with this bug and didn't realize it for a long time...
     
  30. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    Has anyone found an alternative to LoadImageAtPath?
    It is too slow to be used with native photos (.heic format) , about 3/5 seconds to load a single image is way too long
    also LoadImageAtPath cannot be run in a separate thread, not even the android java call

    I have no android coding experience and I wouldn't even know where to start with this.
     
  31. UDN_ef4a5413-8457-4652-a34b-2c29cf75fd7f

    UDN_ef4a5413-8457-4652-a34b-2c29cf75fd7f

    Joined:
    Apr 7, 2019
    Posts:
    11
    Hello everyone,
    how do we save an audio sound in the android and ios gallery?
    thanks for your help :)
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You can't save audio using NativeGallery.
     
  33. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    How can I edit the AAR file?
    I would like to try some things but there is only java files and no project
     
  34. wasonzhu

    wasonzhu

    Joined:
    Mar 23, 2021
    Posts:
    2
    Unity version 2020.2.2, to create an Android application, delete <application... /> tag from AndroidManifest.xml. There is still an error in packaging. How should I solve it? What's the right setting to package an Android app?
     

    Attached Files:

  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    @HEROTECH70 You need to create a new Android Studio project, add those java files to that project, change the project type from application to library, build it and then copy the classes.jar file from the generated aar file to NativeGallery.aar.

    @wasonzhu Why did you delete the application tag in the first place? NativeGallery never prompts you to do so.
     
  36. wasonzhu

    wasonzhu

    Joined:
    Mar 23, 2021
    Posts:
    2
    • I saw this sign
    upload_2021-7-21_9-55-40.png
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You are absolutely right, I don't know what I was thinking. The thing is, you probably modified the wrong AndroidManifest. You must delete that line from NativeGallery.aar's embedded AndroidManifest.

    PS. AFAIK, Google Play already requires target SDK to be set to at least 29, so it would be more advisable to upgrade your Android SDK to get rid of that error.
     
  38. mostafanastary

    mostafanastary

    Joined:
    Oct 4, 2015
    Posts:
    31
    Hi @yasirkula , thanks for plugin in first.

    why we can't save audio ? is there any issue with NativeGallery or any native plugin?
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You can make SaveAudioToGallery functions public in NativeGallery.cs and give them a try but be aware that they won't work on iOS and they may not work on all Android devices since they aren't battle-tested (and probably never will be).
     
  40. hoab

    hoab

    Joined:
    May 1, 2021
    Posts:
    2
    I store "path" in another variable and try to upload an image to google firebase through that variable, but the path obtained from the native gallery may not be correct when used elsewhere?
     
  41. hoab

    hoab

    Joined:
    May 1, 2021
    Posts:
    2
    oh I solved it by adding "file://".
     
    yasirkula likes this.
  42. JulioNicacio

    JulioNicacio

    Joined:
    Jan 3, 2021
    Posts:
    3
    With this plugin I can read a folder from the Android gallery where I saved the photos taken in the application? In this case, without the need to open the gallery, what I need is to simply Get the images in the folder to be able to place them in buttons in a gallery screen within the application itself.

    The code where I save the images:

    Code (CSharp):
    1. public static string SaveImgToGallery(string path, Texture2D texture)
    2.         {  
    3.             string fullName = "SS_" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
    4.  
    5.             NativeGallery.SaveImageToGallery(texture, path, fullName);
    6.            
    7.             return fullName;
    8.         }
    9.  
    10.         public static void RetrieveImgToGallery(string fullName)
    11.         {
    12.             NativeGallery.GetImageFromGallery(null, fullName);
    13.         }
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    There isn't a %100 working solution for this. You can use SaveImageToGallery's MediaSaveCallback to get saved image's path but as stated in documentation, it can return abstract paths if raw filepath can't be determined (hence it isn't %100 what you're looking for).
     
  44. hieutran196

    hieutran196

    Joined:
    Dec 17, 2018
    Posts:
    21
    Hi, thanks for the great asset.
    Can I ask some things?
    May I turn off Access Gallery Dialog on android and just open it when I use your asset? Currently, it appears when first start the app. But I just want to appear when I call get Image from android.
    Thank you.
    ------------------------
    Edit:
    I changed Write permission from External(SD Card) to Internal, and check on Android, Read Permission does not appear when first open the app and working really well (I write texture to the device by
    Code (CSharp):
    1. System.IO.File.WriteAllBytes
    , not use your asset).
    But your document recommends set External(SD Card).
    Is my way oke when I set Internal write?
    Thanks for reply.
     
    Last edited: Aug 22, 2021
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  46. fungzilong

    fungzilong

    Joined:
    Jul 17, 2019
    Posts:
    4
    Hi there,
    Can someone please give me a hand; Im trying to Make a UI Button that if pressed, it will open up the gallery to choose a picture on Andriod.
    I tried implementing the example code, void PickImage(), but unfortunately it doesnt work when i built the app and loaded on the phone. However, in Unity it works (it opens up the PC file directory and can select an image)....
    How would i change the path so that i can find the images on Andriod?
    I did change the write permission to External..
    If someone can please help me, your boy is confused on how to use this :(
    Thanks in Advance
     

    Attached Files:

    Last edited: Aug 22, 2021
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    I'd first recommend you to download latest version from GitHub, just in case. If the code does absolutely nothing on Android, then you should use Unity's Android Logcat package to see what error messages are logged there the moment you call PickImage. If the code partially works (image picker shows up) but doesn't work as intended for you, then you should elaborate on how it isn't working for you.
     
  48. fungzilong

    fungzilong

    Joined:
    Jul 17, 2019
    Posts:
    4
    I will give that a try, thanks for the quick response Yasirkula. and thanks for a free helpful package mate. will let ya know how i go.. cheers

    --------------------------------------
    EDIT Update:
    Thanks Yasirkula, i managed to GetImage and display it on quad- I changed my project from 3D to 2D and it worked.
     
    Last edited: Aug 31, 2021
    yasirkula likes this.
  49. Intraterrestrial_

    Intraterrestrial_

    Joined:
    Jun 27, 2021
    Posts:
    20
    Im testting Native Gallery in Windows but it doesnt work in the editor, when i call

    NativeGallery.SaveImageToGallery(Texture2d t2d, "NameFolder", "App_Capture{0}.png");

    after that call, i check the folder in

    C:\Users\User\AppData\LocalLow\Company\AppName\NGallery
    but nothing is there, is empty!
    I tested on android and it work, only on the editor doesnt saves anything.

    EDIT: I solved by going to NativeGallery.cs and then in line 437 edit

    #if UNITY_EDITOR
    Debug.Log( "SaveToGallery called successfully in the Editor.");
    File.WriteAllBytes( path, mediaBytes ); //<----------- Add this line to fix
    #else
    File.WriteAllBytes( path, mediaBytes );
    #endif
     
    Last edited: Sep 3, 2021
    yasirkula likes this.
  50. fungzilong

    fungzilong

    Joined:
    Jul 17, 2019
    Posts:
    4
    Hey Yasirkula, After obtaining the image from GetImageFromGallery and loading it to a Quad- Do you have a method to save that Image Path so that when a user quit and launches the App,it will automatically appear instead of repeating the GetImageFromGallery process.
    If not, have you got any tips to do so, maybe through PlayerPref? that would be much appreciated please. Thanks bud