Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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,865
    You can use NativeGallery.LoadImageAtPath to get a Texture2D from the image file and assign it to a RawImage. If you have to assign the image to an Image, you can use Sprite.Create to create a Sprite from the Texture2D and assign this sprite to the Image.
     
  2. chayan_b

    chayan_b

    Joined:
    Jun 21, 2019
    Posts:
    2
    Please share your code
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  4. tpathak

    tpathak

    Joined:
    Jun 29, 2018
    Posts:
    3
    How can I increase resolution of screenshot texture which is captured using this plugin?
     
  5. tpathak

    tpathak

    Joined:
    Jun 29, 2018
    Posts:
    3
    Hello, Thanks for fantastic plugin. I want one help.
    I want to pick an image using this plugin. I am using following code to pick an image :

    private void PickImage( int maxSize )
    {

    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery( ( path ) =>
    {
    Debug.Log( "Image path: " + path );
    if( path != null )
    {
    // Create Texture from selected image
    Texture2D texture = NativeGallery.LoadImageAtPath( path, maxSize );
    if( texture == null )
    {
    Debug.Log( "Couldn't load texture from " + path );
    return;
    }

    if(image.texture!=null)
    Destroy(image.texture);

    screenshotViewer.SetActive(true);

    image.texture = texture;

    image.SetNativeSize();
    image.transform.localScale = new Vector3(5,5,5);
    }
    }, "Select a PNG image", "image/png", maxSize );

    Debug.Log( "Permission result: " + permission );
    }

    Due to this code I am able to pick image from any folder from Gallery.
    Now my requirement is to open specific folder from gallery and pick any photo from that folder. Is it possible using this plugin? If yes how can we do this?
     
  6. admin_gzoe

    admin_gzoe

    Joined:
    Dec 6, 2018
    Posts:
    3
    Thank you for sharing a great plugin.
    Can this UI be displayed in landscape mode?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @tpathak You can call ScreenCapture.CaptureScreenshotAsTexture instead of using my method to capture the screenshot: https://docs.unity3d.com/ScriptReference/ScreenCapture.CaptureScreenshotAsTexture.html

    It is not possible to open a specific folder, unfortunately.

    @admin_gzoe On iOS, you can try adding the following line to here: https://github.com/yasirkula/UnityN...ugins/NativeGallery/iOS/NativeGallery.mm#L308 (credit)

    Code (CSharp):
    1. [imagePicker setModalPresentationStyle: UIModalPresentationOverCurrentContext];
    or

    Code (CSharp):
    1. imagePicker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
     
  8. DORNE

    DORNE

    Joined:
    Jul 4, 2017
    Posts:
    10
    Hi,
    Could you tell me if the function NatShare.Share can share gif files on both Ios and Android ?
    Thanks
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  10. BLT_Dev

    BLT_Dev

    Joined:
    Apr 14, 2018
    Posts:
    4
    Question: When saving to the gallery and using the formatting feature (image_[0] for example), how do I know what it named the file? I wan't to use it's path/filename for sharing on social media.
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    NativeGallery doesn't support returning the saved image's path. You'll have to save the image to a temporary location like Application.temporaryCachePath and then share the image at that path.
     
  12. admin_gzoe

    admin_gzoe

    Joined:
    Dec 6, 2018
    Posts:
    3
    @yasirkula
    I succeeded in displaying in landscape mode.
    Thank you very much.
     
    yasirkula likes this.
  13. smartinetz

    smartinetz

    Joined:
    Dec 8, 2017
    Posts:
    49
    Hey..

    thanks for the great plugin first of all!!!

    I do have a few simple questions about jpeg files on iOS...
    1. Why are jpeg/jpg images converted into PNGs on iOS?
    2. It seems after the converted PNGs are copied to the "Library/Caches/" folder, the original images remain in a folder named "tmp" which is on the same level as the "Library" and "Documents" folders. Is this folder maintained by the OS and cleaned by itself or is this a bug?
    I am out of ideas how to reach this folder from inside Unity.

    Regards
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    1. I don't check the type of the picked image. I think I didn't find a proper way to check the type back when I was writing the plugin. I also need to export the image in either JPEG or PNG since there doesn't seem to be a way to simply copy the source image from Photos to a target path while using UIImagePickerController, please correct me if you find a solution that doesn't use UIImagePNGRepresentation or UIImageJPEGRepresentation.

    Considering these two points, I export the image in PNG format since JPEG doesn't support transparency.

    2. It might be related to our discussion with @blaumeisje, starting with this message: https://forum.unity.com/threads/nat...id-ios-open-source.519619/page-9#post-4735337

    It might be a bug but I don't know what is causing it.
     
    Deleted User likes this.
  15. smartinetz

    smartinetz

    Joined:
    Dec 8, 2017
    Posts:
    49
    Hey, thanks for the answer and I too think it's a bug that causes the overall APP size to decrease over time. I am using an iOS File Browser (iMazing) to see what exactly happens on my device. You can reconstruct the bug by taking a picture using the iPhone/ipad camera, then start the unity APP and use your plugin to add that picture into the app.

    Since it's a JPEG image, a copy of the original file ends up in a folder named "tmp" inside the app. According to Apple, this folder will be maintained and cleaned by the system after restart but I have my doubts. And not every user ist restarting their device every day/week.

    Do you possibly know of a way to access this "TMP" folder since it's outside of the "Application.persistantDataPath"
    range?!

    Regards
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  17. SCADEng

    SCADEng

    Joined:
    Jul 17, 2017
    Posts:
    1
    Dear @yasirkula I'm recently downloading your plugins for an android app. The problem is the GetImageFromGallery function doesn't work. My gallery doesn't open and i cant pick image. In more, the call back function is not called
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Did you set Write Permission to External (SDCard) in Player Settings? If you are using ProGuard minification, try adding
    -keep class com.yasirkula.unity.* { *; }
    to your ProGuard filters. If it is still not working, please check logcat for any meaningful error messages.
     
  19. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
    Hi, thanks for work and it's free for us.
    Can you add photo editor for an example user pick up photo for avatar, please =)
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    yuliyF likes this.
  21. FatEagleGames

    FatEagleGames

    Joined:
    Sep 8, 2019
    Posts:
    2
    Hi @yasirkula, first of all thank you for your amazing plugins.

    Just a quick heads-up for everyone having the same problem as me:
    Last week my code using Native Gallery started throwing UnauthorizedAccessExceptions when selecting images from the gallery and i finally found out why.

    In Android 10 (API level 29), the permissions for accessing the external storage were changed. https://developer.android.com/training/data-storage/files/external-scoped
    Apps can now only access the app-specific directory, even with the READ_EXTERNAL_STORAGE permission set.
    Luckily, there is a workaround by adding the following code to the AndroidManifest.xml:

    Code (CSharp):
    1. <manifest ... >
    2.   <!-- This attribute is "false" by default on apps targeting
    3.        Android 10 or higher. -->
    4.   <application android:requestLegacyExternalStorage="true" ... >
    5.     ...
    6.   </application>
    7. </manifest>
    Since this workaround will not be supported in future Android versions, is there anything you can do to make your plugin still work with Android 10+?

    Best Regards
     
    yasirkula and yuliyF like this.
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Thank you for bringing this to my attention. For reference, were you using GetImageFromGallery or GetImagesFromGallery? If it is the latter and you get the chance to test the GetImageFromGallery function to see if it suffers from the same issue, it would be much appreciated.
     
  23. FatEagleGames

    FatEagleGames

    Joined:
    Sep 8, 2019
    Posts:
    2
    I just tested it, without the workaround both GetImageFromGallery and GetImagesFromGallery throw the same error. With the workaround applied, both methods work perfectly fine. Thanks for looking into it :)
     
    yuliyF and yasirkula like this.
  24. MaryamKamel

    MaryamKamel

    Joined:
    Feb 5, 2016
    Posts:
    22
    can i use it to open a specific file in the gallery and view the images inside without picking any image?
    i mean can i use it as a gallery launcher ??
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    It is not supported, sorry. A workaround for Android was mentioned a couple of times in the previous posts, so you can try to find them if you feel adventurous enough. Though, I might add this feature in a future update since it is being requested a lot of times.
     
    MaryamKamel likes this.
  26. woodsun

    woodsun

    Joined:
    Sep 16, 2013
    Posts:
    4
    Hi, yasirkula
    Thank you for great asset! It became my lifesaver.

    Could you please show me a sample code to get value of MediaSaveCallback(null)? I want to start some commands after image is saved on mobile storage. Thanks in advance.
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Code (CSharp):
    1. NativeGallery.SaveImageToGallery( mediaBytes, "album", "img {0}.png", ( error ) =>
    2. {
    3.     // Execute post-save operations here
    4.    
    5.     if( string.IsNullOrEmpty( error ) )
    6.     {
    7.         // Image was saved successfully
    8.     }
    9. } );
     
  28. woodsun

    woodsun

    Joined:
    Sep 16, 2013
    Posts:
    4
    Thank you so much!!

     
    yasirkula likes this.
  29. MaryamKamel

    MaryamKamel

    Joined:
    Feb 5, 2016
    Posts:
    22
    okay i will try to find the workaround in the previous comments as its a very important task for me ,
    thank you so much :D
     
  30. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Hello, I would like to allow the user to choose audio files like MP3 or other music files. Can this asset do that? Or how can I do that?

    I just want to pup up a screen allowing the user to choose any MP3 or other audio files he has on his phone, and be able to pick one. All it has to show is the file names of the audio files, no previews or any fancy graphics is needed
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Picking audio files is not supported :/
     
  32. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Can picking audio files be added? Can the existing one just filter the files and show those like *.mp3 I dont understand why it cant do that? its just a bunch of files isnt it?
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  34. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    AFAIK, there is no built-in UI (that has an API) to pick audio files on iOS. On Android, you need to change the lines I've mentioned, instead of adding new lines. That means you'll have to sacrifice the ability to pick either images or videos.
     
  36. Taras-Hulka

    Taras-Hulka

    Joined:
    Oct 24, 2014
    Posts:
    5
    Hello, Thanks for the plugin.
    It works perfectly. But I have a problem with iOS 13. When I choose a video, I'll get this path
    /private/var/mobile/Containers/Data/PluginKitPlugin/76694158-2407-4725-BFC6-CD2E7B62FE3B/tmp/trim.C8D9D4D0-4DECEFD6A...

    But when I try to use this path I'll get the error "file not found"

    On IOS 12 path looks like this
    /private/var/mobile/Containers/Data/Application/0AA0156D-CB08-4AE0-986F-8258BCF9FA03/tmp/28ADC490-BFAB-4957-A67B-14DECEFD6A29.MOV

    I think the issue is in “PluginKitPlugin” – temporary video file created in shared folder and application doesn’t have permission to this folder

    The issue is only on IOS 13 and only with video. The image picker works right.
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Taras-Hulka likes this.
  38. ARAMMDE

    ARAMMDE

    Joined:
    Oct 5, 2019
    Posts:
    2
    Hi. I am not using the ProGuard or any other special tool beside Unity. Anyway I get "AndroidJavaException: java.lang.ClassNotFoundException:com.yasirkula.unity.NativeGalleryPermissionReceiver" inside logcat.

    My version is Unity 2019.1 hence I didn't grab the "Native Gallery" from the Unity asset store but from Github a few days ago. (are ther differences?) I am using Unity integrated Android NDK and SDK!
    I find an other thread about Unity 2019.1 and .2 regrading "ClassNotFoundException" which may be simmilar to this:
    https://github.com/Unity-Technologies/GooglePlayLicenseVerification/issues/9
    There it is mentioned that it might be related to NDK which has been r19b on release but then was reverted to r16b internally by unity because noone could make IL2CPP builds for Android anymore. I just upgraded a few weeks ago to Unity 2019.1 so I asume I have r16b.
    Any idea?
    best regrads
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    There's also a Gradle (Experimental) minification setting. Maybe it is selected in Player Settings? Other than that, I'm not sure why this issue occurs. You can verify the existence of the NativeGalleryPermissionReceiver class by opening Assets/Plugins/NativeGallery/Android/NativeGallery.jar with Winrar.
     
  40. jhegarty_gsn

    jhegarty_gsn

    Joined:
    Jan 9, 2018
    Posts:
    1
    Hi @yasirkula
    Thanks very much for the plugin. Are you still supporting this?
    I'm trying to save an image to gallery on Android and iOS. On Android its working perfectly fine, but on iOS I'm getting a grey screen and an error message: "UnauthorizedAccessException: Access to the path"
    And it seems to be trying to write to "/var/mobile/Containers/Data/Application/....."

    I've added this as per the documentation:

    Code (CSharp):
    1.  
    2. private const string PHOTO_LIBRARY_USAGE_DESCRIPTION = "Save media to Photos";
    3. private const bool MINIMUM_TARGET_8_OR_ABOVE = true;
    4.  
    And this is my implementation:
    Code (CSharp):
    1.  
    2. if (NativeGallery.CheckPermission() != NativeGallery.Permission.Granted)
    3. {
    4.     NativeGallery.RequestPermission();
    5. }
    6.  
    7. if (NativeGallery.SaveImageToGallery(snapshot.ByteArray, "testalbum", fileName) == NativeGallery.Permission.Denied)
    8. {
    9.     Debug.LogError("Couldn't save image, permission denied");
    10. }
    11.  
    I'd appreciate any help you can give, thanks!
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @FatEagleGames @nsmith1024 @Taras-Hulka The latest update on GitHub addresses these issues: https://github.com/yasirkula/UnityNativeGallery/releases. Asset Store version will be updated soon.

    @jhegarty_gsn It might be related to another plugin or using ScreenCapture.CaptureScreenshot which doesn't create the screenshot file immediately. For reference:

    - https://github.com/yasirkula/UnityNativeGallery/issues/63
    - https://docs.unity3d.com/ScriptReference/ScreenCapture.CaptureScreenshot.html

    You might also want to check the latest update on GitHub, maybe it somehow fixes this issue, as well.
     
  42. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
    When trying pick image:
    Code (CSharp):
    1. NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    2. {
    3. ...
    you see an Android window asking a permission. If pressed deny my apps lose focus and users think that apps is broke
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  44. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
  45. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
    Code (CSharp):
    1.  NativeGallery.GetImageFromGallery(GetImageFromGalleryResult);
    Done, it was fixed after update)
     
    yasirkula likes this.
  46. brklemz

    brklemz

    Joined:
    Nov 26, 2015
    Posts:
    14
    Do you have a "SaveVideoToGallery" example I can look at. I am using your NativeCamera.RecordVideo to record a video (which works well) but I can't seem to find an example of how to save this video to the gallery. I see loads of "SaveImage" examples here and on GitHub, but no "SaveVideo" to gallery examples.

    Thanks in advance.

    Here is a code snippet:

    //takevideo button pressed
    public void GotoTakeVideo()
    {
    // Don't attempt to use the camera if it is already open
    Debug.Log("in take");
    if (NativeCamera.IsCameraBusy())
    return;
    RecordVideo();
    }

    private void RecordVideo()
    {
    //grab video and replay it, this works perfectly
    NativeCamera.Permission permission = NativeCamera.RecordVideo((path) =>
    {
    Debug.Log("Video path: " + path);
    if (path != null)
    {
    // Play the recorded video
    Handheld.PlayFullScreenMovie("file://" + path);
    }
    });
    Debug.Log("Permission result: " + permission);

    //save video to gallery
    //??confused on what goes here so I can store taken videos??

    }
     
    Last edited: Oct 16, 2019
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    There you go:
    NativeGallery.SaveVideoToGallery( path, "Album Name", /*filename*/ System.IO.Path.GetFileName(path) );
     
  48. brklemz

    brklemz

    Joined:
    Nov 26, 2015
    Posts:
    14
    WOW, works perfectly. I love that I can pick the album and save all the app's videos there. Very organized.

    I can't thank you enough, Bruce
     
    yasirkula likes this.
  49. shefal89

    shefal89

    Joined:
    Jun 20, 2019
    Posts:
    7
    how to use this library?
     
  50. shefal89

    shefal89

    Joined:
    Jun 20, 2019
    Posts:
    7
    will this library work on AR APP?