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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Native Gallery for Android & iOS [Open Source]

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

  1. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Thanks, ill try doing it.
     
  2. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, just to be sure, i download the package from the package manager, then i go to Assets/Plugins/Android/Proguard.txt , then i add -keep class com.yasirkula.unity.* { *; } there right? Cause the file seems to be empty
     
  3. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    T
    Thanks, working fine!
     
    yasirkula likes this.
  4. shrirammvasudevan

    shrirammvasudevan

    Joined:
    Mar 31, 2020
    Posts:
    9
    Hey Yasirkula.

    I tried to use "Texture2D texture = NativeGallery.LoadImageAtPath(path);" However, "texture" does not seem to be readable and that is causing some issues for me. Would you happen to know a fix for this?
     
    Last edited: Mar 23, 2021
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    Yes, LoadImageAtPath takes an optional markTextureNonReadable parameter.
     
  6. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, can i use this plugin for text files as well?
     
  7. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, i am getting this problem, so i guess that an empty file path is being returned before the user has finished selecting the file, in the editor this work great but problem on build android Prob 3.PNG
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
  9. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
  10. shrirammvasudevan

    shrirammvasudevan

    Joined:
    Mar 31, 2020
    Posts:
    9
    Im not sure if this is because of my lack of experience but I tried to do:

    Texture2D texture = NativeGallery.LoadImageAtPath(path, bool markTextureNonReadable = false);

    I get an error, "markTextureNonReadable" does exist in the current context.
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    It should be
    Texture2D texture = NativeGallery.LoadImageAtPath(path, markTextureNonReadable: false);
    .
     
  12. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, i am getting an error : out of memory when loading the image from a file. Why that?
     
  13. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, also, when i want to pick a text file with the NativeFilePicker, it says that i have no app to pick one... Can't i just call the same file picker as the image one?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    Out of memory usually means that your game's RAM usage has exceeded the free RAM allocated to your game. You must pass a small maxSize parameter to LoadImageAtPath if you don't want the Textures to consume too much RAM.

    NativeGallery invokes Android's native user interface, so I didn't create that UI. If NativeFilePicker fails to find an application for text files (I must say that I find it unlikely, you might be doing something wrong), we can't do anything about this. You can use SimpleFileBrowser as a last resort: https://github.com/yasirkula/UnitySimpleFileBrowser
     
  15. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    This is what it shows Screenshot_20210326-211736[1].png
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    I'll need to see how you are calling NativeFilePicker functions.
     
  17. shrirammvasudevan

    shrirammvasudevan

    Joined:
    Mar 31, 2020
    Posts:
    9
    Sorry for another follow up but now Im getting a "Not Implemented" error after passing the parameter the way you mentioned above.
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    Can I see full stacktrace?
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    What is "Not Implemented" error's full stacktrace?
     
  20. shrirammvasudevan

    shrirammvasudevan

    Joined:
    Mar 31, 2020
    Posts:
    9
    My mistake, it had to do with something else. Nothing related to Native Gallery.
     
    Last edited: Jul 12, 2021
    yasirkula likes this.
  21. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    177
    I have imported the asset from the asset store twice and I am getting this issue... Any other ideas?

    I changed to my "blank sample project" and it worked just fine...so I am guessing there is some sorta conflict happening..

    Now it is working...not even sure why I did not change anything except I created a test script...

    Well hey @yasirkula I appreciate you making this plugin and having as an open sourced asset... you the best.
     
    Last edited: Mar 29, 2021
    yasirkula likes this.
  22. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    301
    Hi Yasirkula Your package is superb. In my project i have to share a shortlink..with a text . I have a create a short link. Its like a https://wa.link/z32nim-(it is changed). It displays my site. When i click the image it goes to playstore. But when i use the the below code.

    it just displays the text. Not a Thumbnail view of the website. How can i do it. I have to view the thumbnail while sharing .
    how can i do it.

    thanks in advance
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
  24. TriuneGrandeur

    TriuneGrandeur

    Joined:
    Nov 20, 2020
    Posts:
    3
    Hi, I am using your package in my game. It works fine in unity but when I try it on mobile the image is not being imported properly. Its giving a pink image, probably not being able to read textures. Is there something you can suggest for this.
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    Pink image means missing shader. If you are testing the example code on SRP, it can happen. You can either assign the Texture to an existing object in your scene, or to a RawImage.
     
  26. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    177
    back from the dead to help others in the future...

    I had the vs code highlighting red and saying "The name NativeGallery does not exist in the current context" as well...

    When I switched my platform from Linux to Android... the red highlighting issue went away...
     
    yasirkula likes this.
  27. GusMoura

    GusMoura

    Joined:
    Jul 7, 2018
    Posts:
    2
    Hello, yasirkula. First of all, congratulations! This is an awesome plugin, very usefull for all of us!
    I'm just a little confused about ios privacy requiriments.
    My game just access the gallery to read the photos (we don't write).
    If I understood correctly, your plugin just include in the plist PHOTO_LIBRARY_USAGE_DESCRIPTION and PHOTO_LIBRARY_ADDITIONS_USAGE_DESCRIPTION, right?
    When I run my app with you plugin, it doesn't ask for Gallery permission, but it works fine on ios and also on android (it opens the gallery as expecteded). It is important to say that my game asks permission for camera and notification because our game needs to use it.
    I got conffused because this abscence of Gallery permission. Do I need it or not?
    I know if I set the variable "PermissionFreeMode" to false it will ask the user for the gallery permission, but my app freezes if he chooses "Select Photos " option (as you said it would). So to set "PermissionFreeMode" to false it is not an good option for us.
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    So it is expected to not see a permission dialog on iOS for picking images but I'd expect the permission dialog to show up on Android. You can delete the app from your Android device, reinstall it, don't grant the permissions at startup and check if Storage permission is asked before accessing the Gallery.
     
    GusMoura likes this.
  29. GusMoura

    GusMoura

    Joined:
    Jul 7, 2018
    Posts:
    2
    On Android it is asking for gallery permission. On ios it`s not. My question was just about ios. So looks like it is working fine! Thanx for you reply!
     
    yasirkula likes this.
  30. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Isnt there a way to pull a thumbnail preview using native cam/gallery?
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    There are LoadImageAtPath and GetVideoThumbnail functions.
     
  32. chaklasiyajaimin

    chaklasiyajaimin

    Joined:
    Mar 11, 2020
    Posts:
    1
    Hey @yasirkula can we load all images from gallery folder wise ???
     
  33. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Thanks Yas. One more question. Is it possible to pull a video/image from the users Gallery, and then save it to local folder within the app? Right now i pull from gallery and upload it to the server. But if the user wants to save to local storage, but the media not be seen in gallery. Like a Keep Safe Vault app works. Keeps all the media hidden from gallery unless you go into the app to view.
     
  34. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Pretty sure using the "load multiple images/videos" is what you want
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    @chaklasiyajaimin You can't pick a folder with this plugin. The closest thing to what you want is GetImagesFromGallery, which lets you pick multiple images (not supported on old devices).

    @gaglabs Well, you can try calling File.Move on returned paths but I can't guarantee a result since this wasn't the use-case I had in mind while designing the plugin.
     
    gaglabs likes this.
  36. EvanMcGrath

    EvanMcGrath

    Joined:
    Dec 1, 2020
    Posts:
    6
    Hello, I'm starting to run into an issue after updating where gifs I've been creating in Unity and saving to the persistent data path give corrupted files when I try to save them on my Android device. I have permission to save, and I know good files are produced, but when saved locally, they are said to be 0 bytes despite being over 6 MB prior to being saved via NativeGallery. I'm certain gif are supported, as this worked properly in older versions, did something change, or am I doing something wrong?
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
  38. EvanMcGrath

    EvanMcGrath

    Joined:
    Dec 1, 2020
    Posts:
    6
    It was last confirmed working on v1.5.3. Logcat is giving me this error during the export process:

    Warn ExifInterface Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface.


    According to the docs for ExifInterface, GIF is unsupported, so this may be the issue. I can try exporting the GIF by a byte array instead, but I'm thinking it may cause the same problem.
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    I'm inspecting the diff here but at the moment, I am unable to find the culprit on Android. You've tested v1.5.3 on the same device that this issue occurs, right? And you've tested it after my previous message? Are you using any of the new stuff like overwriteExistingMedia or mediaSaveOmitDCIM?
     
  40. EvanMcGrath

    EvanMcGrath

    Joined:
    Dec 1, 2020
    Posts:
    6
    I'm not using any of the new functions. I have tested the last known build where the feature was working on the same device and it's functional. It saves properly. We had issues where the JAR could occasionally be removed from the project, which would cause it to stop working, but when the JAR was present, it correctly outputs the gif to my Gallery.
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    JAR is expected to be removed because NativeGallery uses AAR for a while. Since both of these contain same native classes, JAR is removed to avoid duplicate class errors.
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    So you've removed NativeGallery from your project completely, imported v1.5.3 for testing purposes and verified that it has no issues? And then you've removed NativeGallery from your project completely, imported v1.6.0 for testing purposes and verified that it has issues?
     
  43. EvanMcGrath

    EvanMcGrath

    Joined:
    Dec 1, 2020
    Posts:
    6
    I will attempt a fresh install of v1.5.3 soon but a fresh install of v1.6.0 produces the corrupted 0 byte gif.
     
    yasirkula likes this.
  44. EvanMcGrath

    EvanMcGrath

    Joined:
    Dec 1, 2020
    Posts:
    6
    A fresh install with v.1.5.3 also produces the corrupt gif, so the issue is probably on my end after all. Thanks for the help anyway!
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
  46. MrQube

    MrQube

    Joined:
    Sep 1, 2019
    Posts:
    6
    Good morning, yasirkula.

    First of all: thanks for this helpful plugin!

    I am using it in a project which has been released for iOS and Android successfully, but yesterday I received a warning from Google Play related to the permissions requested by the app. Here is what it is about:

    We've detected that your app contains the requestLegacyExternalStorage flag in the manifest file of 1 or more of your app bundles or APKs.

    Developers with apps on devices running Android 11+ must use Scoped Storage to give users better access control over their device storage. To release your app on Android 11 or newer after May 5th, you must either:

    • Update your app to use more privacy friendly best practices, such as the Storage Access Framework or Media Store API
    • Update your app to declare the All files access (MANAGE_EXTERNAL_STORAGE) permission in the manifest file, and complete the All files access permission declaration in Play Console from May 5th
    • Remove the All files access permission from your app entirely
    For apps targeting Android 11, the requestLegacyExternalStorage flag will be ignored. You must use the All files access permission to retain broad access.

    Apps requesting access to the All files access permission without a permitted use will be removed from Google Play, and you won't be able to publish updates.

    https://support.google.com/googleplay/android-developer/answer/10467955

    I am not sure I really understand what this means – do or don't I have to change something when using your plugin?

    Right now, I am on version 1.6.2. with Unity 2020.3 and targeting Android 9 (API 28) and above.

    The only permission I really need is to save a screenshot to storage – not to read from there.

    Thanks and all the best!
     
    Last edited: Apr 15, 2021
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    We've been discussing this just now on GitHub: https://github.com/yasirkula/UnityNativeGallery/issues/182

    My assumption is that requestLegacyExternalStorage won't cause an issue after setting target SDK to Android 11 but at the moment, I can't say for certain because the announcement is puzzling and if we were to interpret it as is, it contradicts with previous announcements, as you can see from the linked discussion.

    That being said, you don't need requestLegacyExternalStorage for simply saving media to Gallery.
    So you can open NativeGallery.aar with WinRAR or 7-Zip and then remove the
    <application ... />
    tag from AndroidManifest.xml.
     
  48. SamAgten

    SamAgten

    Joined:
    Jul 31, 2012
    Posts:
    2
    Hi @yasirkula ! thanks for the great plugin!

    We're running into an issue while trying to load a video from the gallery using the plugin. We tried to trim it down as much as possible and ended up with the following:

    Code (CSharp):
    1. private static void LoadVideoFromGallery()
    2.     {
    3.         NativeGallery.Permission permission = NativeGallery.GetVideoFromGallery((path) =>
    4.         {
    5.             Debug.Log("Video path: " + path);
    6.             if (path != null)
    7.             {
    8.                 Debug.Log("File exists: " + File.Exists(path));
    9.                 if (videoChosen != null)
    10.                     videoChosen(path);
    11.             }
    12.         }, Localization.GetValue("popup.chosevideo"));
    13.  
    14.         Debug.Log("Permission result: " + permission);
    15.     }
    The video path that is returned seems to be a valid path in the form of: /private/var/mobile/Containers/.../tmp/trim.32F75E1C-D5B7-4FFE-AA8B-81E2E25F9B8C.MOV

    But any file operation we try to do on this file fails. In fact, the following line (File.Exists) returns false... It's worth noting that the permission does return as "Granted".

    Am I missing something obvious here? The code works just fine on Android.

    Thank you in advance!
     
    stilkin likes this.
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,786
    Can you see any error messages in Xcode console? On which iOS version does this occur? Which Xcode version and target SDK version are you using? Can you reproduce the issue on different iOS devices with different iOS versions?
     
    stilkin likes this.
  50. stilkin

    stilkin

    Joined:
    Jan 6, 2021
    Posts:
    4
    Hi there, thanks for your swift response!

    I am @SamAgten 's colleague; here are some answers to your questions:
    • iOS devices: iPhone X and iPhone 6
    • iOS version: 14.4.1 and 12.5.1 (respectively)
    • XCode version: 12.4 (12D4e)
    • iOS Deployment Target: 10.0
    • Unity: 2019.4.19f1
    Some error logs:
    Code (CSharp):
    1. Permission result: Granted
    2. MediaLoader:LoadVideoFromGallery()
    3. UnityEngine.Events.UnityAction:Invoke()
    4. // ...
    5.  
    6. [586:72013] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
    7. Video path: /private/var/mobile/Containers/Data/PluginKitPlugin/DE0233F1-0487-4EED-A4BA-D2E8B5965CE6/tmp/trim.32F75E1C-D5B7-4FFE-AA8B-81E2E25F9B8C.MOV
    8. <>c:<LoadVideoFromGallery>b__11_0(String)
    9. MediaPickCallback:Invoke(String)
    10. NativeGalleryNamespace.NGMediaReceiveCallbackiOS:OnMediaReceived(String)
    11. //...
    12.  
    13. File exists: False
    14. <>c:<LoadVideoFromGallery>b__11_0(String)
    15. MediaPickCallback:Invoke(String)
    16. NativeGalleryNamespace.NGMediaReceiveCallbackiOS:OnMediaReceived(String)
    (let me know if these are sufficient)